Can't subclass datetime.datetime?

Grant Edwards grante at visi.com
Mon Feb 14 14:49:08 EST 2005


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!



More information about the Python-list mailing list