Can't subclass datetime.datetime?

G

Grant Edwards

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?
 
S

Steven Bethard

Grant said:
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
 
G

Grant Edwards

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.
 
K

Kent Johnson

Grant said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,813
Messages
2,569,696
Members
45,486
Latest member
Deanna5597

Latest Threads

Top