473,387 Members | 1,476 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Can't subclass datetime.datetime?

Is it true that a datetime object can convert itself into a
string, but not the other way around? IOW, there's no simple
way to take the output from str(d) and turn it back into d?

So, I tried to create a class that knows how to do that, but I
don't seem to be able to subclass datetime.datetime:

import datetime

class MyDatetime(datetime.datetime):
def __init__(self,s):
s1,s2 = s.split(' ')
v = s1.split('-') + s2.split(':')
v = map(int,v)
datetime.datetime.__init__(self,v[0],v[1],v[2],v[3],v[4],v[5])

s = '2005-02-14 12:34:56'
d = MyDatetime(s)

Running the above yields:

Traceback (most recent call last):
File "dt.py", line 11, in ?
d = MyDatetime(s)
TypeError: function takes at least 3 arguments (1 given)

What's going on?

--
Grant Edwards grante Yow! I'm in a twist
at contest!! I'm in a
visi.com bathtub! It's on Mars!! I'm
in tip-top condition!
Jul 18 '05 #1
3 2949
Grant Edwards wrote:
Is it true that a datetime object can convert itself into a
string, but not the other way around? IOW, there's no simple
way to take the output from str(d) and turn it back into d?
I assume this is true because there is not one standard format for a
date-time string. But I don't use the module enough, so I'll let
someone else answer this part of the question.
import datetime

class MyDatetime(datetime.datetime):
def __init__(self,s):
s1,s2 = s.split(' ')
v = s1.split('-') + s2.split(':')
v = map(int,v)
datetime.datetime.__init__(self,v[0],v[1],v[2],v[3],v[4],v[5])

s = '2005-02-14 12:34:56'
d = MyDatetime(s)

Running the above yields:

Traceback (most recent call last):
File "dt.py", line 11, in ?
d = MyDatetime(s)
TypeError: function takes at least 3 arguments (1 given)


datetime.datetime objects are immutable, so you need to define __new__
instead of __init__:

py> class DateTime(datetime.datetime):
.... def __new__(cls, s):
.... s1, s2 = s.split(' ')
.... v = map(int, s1.split('-') + s2.split(':'))
.... return datetime.datetime.__new__(cls, *v)
....
py> DateTime('2005-02-14 12:34:56')
DateTime(2005, 2, 14, 12, 34, 56)

Steve
Jul 18 '05 #2
On 2005-02-14, Steven Bethard <st************@gmail.com> wrote:
Grant Edwards wrote:
Is it true that a datetime object can convert itself into a
string, but not the other way around? IOW, there's no simple
way to take the output from str(d) and turn it back into d?
I assume this is true because there is not one standard format
for a date-time string.


There seems to be a de-facto standard format: that which is
returned by the str(d).
class MyDatetime(datetime.datetime):
def __init__(self,s):

[...]
datetime.datetime objects are immutable, so you need to define
__new__ instead of __init__:


Thanks. I should have known that. I guess I've never subclassed
an immutible type before.

--
Grant Edwards grante Yow! It's hard being
at an ARTIST!!
visi.com
Jul 18 '05 #3
Grant Edwards wrote:
Is it true that a datetime object can convert itself into a
string, but not the other way around? IOW, there's no simple
way to take the output from str(d) and turn it back into d?


According to this thread, a patch has been checked in that adds strptime() to datetime. So there is
something to look forward to...
http://tinyurl.com/4fbkb

Kent
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Gerry Sutton | last post by:
Hi All! I have noticed a strange behavior when using a constant identifier to initialize an instance list variable in a base class and then trying to modifying the list in subclasses by using...
44
by: Frank Rizzo | last post by:
Any ideas?
2
by: ToChina | last post by:
Hi, I have the following code: class A { } class B : A { }
8
by: Lou Pecora | last post by:
I've been scanning Python in a Nutshell, but this seems to be either undoable or so subtle that I don't know how to do it. I want to subclass a base class that is returned from a Standard Library...
31
by: damacy | last post by:
hi, there. i have a problem writing a program which can obtain ip addresses of machines running in the same local network. say, there are 4 machines present in the network; , , and and if i...
3
by: davidfinance | last post by:
Ok, maybe this is a stupid question, but why can't I make a subclass of datetime.date and override the __init__ method? --- from datetime import date class A(date): def __init__(self, a,...
6
by: Me | last post by:
I need to be able to acces non-virtual members of sublcasses via a base class pointer...and without the need for an explicit type cast. I thought a pure virtual getPtr() that acts as a type cast...
4
by: Kurt Smith | last post by:
Hi List: Class inheritance noob here. For context, I have the following base class and subclass: class Base(object): def __init__(self, val): self.val = val
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.