Why doesn't this work?
Larry Bates
larry.bates at websafe.com
Sat Oct 21 12:53:18 EDT 2006
Because datetime is a new-style class:
The Constructor __new__
If you are like me, then you probably always thought of the __init__ method as
the Python equivalent of what is called a constructor in C++. This isn't the
whole story.
When an instance of a class is created, Python first calls the __new__ method of
the class. __new__ is a static method that is called with the class as its first
argument. __new__ returns a new instance of the class.
The __init__ method is called afterwards to initialize the instance. In some
situations (think "unplickling"!), no initialization is performed. Also,
immutable types like int and str are completely constructed by the __new__
method; their __init__ method does nothing. This way, it is impossible to
circumvent immutability by explicitly calling the __init__ method after
construction.
I think what you wanted was:
>>> class ts(datetime):
... def __new__(self): pass
...
>>> a=ts()
-Larry
More information about the Python-list
mailing list