[Pythonmac-SIG] Extending datetime
Joel Bender
jjb5 at cornell.edu
Wed Mar 17 10:26:57 EST 2004
I'm having trouble extending the datetime class. My suspicion is
that datetime is not a real class, but something else. And this
probably isn't a Mac specific thing.
Python 2.3 (#2, Jul 30 2003, 11:45:28)
[GCC 3.1 20020420 (prerelease)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> datetime(1,2,3)
datetime.datetime(1, 2, 3, 0, 0)
So far, so good. The class constructor needs at least three parameters.
>>> class X(datetime):
... def __init__(self):
... datetime.__init__(self,1,2,3)
...
My class X extends datetime and provides them.
>>> X()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: function takes at least 3 arguments (0 given)
Huh?
>>> X(4,5,6)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: __init__() takes exactly 1 argument (4 given)
This error I expect, X.__init__ should have only one argument. Now I
try something else:
>>> class Y(datetime):
... def __init__(self,*args):
... print "!", args
... datetime.__init__(self,1,2,3)
...
My intent is to parse the args at some point and initialize the base
class in a variety of different ways.
>>> Y()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: function takes at least 3 arguments (0 given)
I still don't understand, but at least it's consistent :).
>>> Y(4,5,6)
! (4, 5, 6)
Y(4, 5, 6, 0, 0)
Looks good so far.
>>> y = Y(4,5,6)
! (4, 5, 6)
>>> str(y)
'0004-05-06 00:00:00'
Huh? Isn't the year supposed to be 1?
Joel
More information about the Pythonmac-SIG
mailing list