datetime objects and __new__()
Peter Otten
__peter__ at web.de
Tue Nov 25 11:16:30 EST 2008
peter wrote:
>> >>> from datetime import *
>> >>> class TS(datetime):
>>
>> ... def __new__(cls, ts):
>> ... return datetime.fromtimestamp(ts)
>> ...>>> TS(0)
>>
>> datetime.datetime(1970, 1, 1, 1, 0)
>>
>> works super() would be the most likely culprit.
>
> Yes, that works, except the returned object is (unsurprisingly) a pure
> datetime instance, which means I cannot access any other attributes I
> defined on my class.
How about
import datetime
class DT(datetime.datetime):
def __new__(cls, *args):
if len(args) == 1:
return cls.fromtimestamp(args[0])
return datetime.datetime.__new__(cls, *args)
then?
Peter
More information about the Python-list
mailing list