datetime objects and __new__()

P

peter

Hi --
.... foo = 'bar'
.... def __new__(cls, s):
.... c = super(ts, cls)
.... return c.fromtimestamp(s)
....Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in __new__
TypeError: __new__() takes exactly 2 arguments (9 given)

I don't understand why that happens -- am I correct in assuming that
the call to .fromtimestamp() is picking up on the ts class? Shouldn't
it get the datetime class instead?

(Yes, I am aware of the problems of using datetime and timestamps)

Could some kind soul please enlighten me?

peter.
 
P

Peter Otten

peter said:
... foo = 'bar'
... def __new__(cls, s):
... c = super(ts, cls)
... return c.fromtimestamp(s)
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in __new__
TypeError: __new__() takes exactly 2 arguments (9 given)

I don't understand why that happens -- am I correct in assuming that
the call to .fromtimestamp() is picking up on the ts class? Shouldn't
it get the datetime class instead?

(Yes, I am aware of the problems of using datetime and timestamps)

Could some kind soul please enlighten me?

If the datetime class were implemented in Python the fromtimestamp() method
could look like:

@classmethod
def fromtimestamp(cls, s):
year, month, day,... = ...
return cls(year, month, day,...)

This will fail since you modified the constructor to accept only a single
argument.

Peter
 
P

peter

If the datetime class were implemented in Python the fromtimestamp() method
could look like:

@classmethod
def fromtimestamp(cls, s):
    year, month, day,... = ...
    return cls(year, month, day,...)

This will fail since you modified the constructor to accept only a single
argument.

Hm, I had hoped that using super() would result in calling the
constructor of the superclass, ie. datetime. Did I use super() wrong?

Thanks,
peter.
 
P

Peter Otten

peter said:
Hm, I had hoped that using super() would result in calling the
constructor of the superclass, ie. datetime. Did I use super() wrong?

Thanks,
peter.

Sorry, I didn't pay the necessary attention.

I've only used super() with "normal" methods, but as
.... def __new__(cls, ts):
.... return datetime.fromtimestamp(ts)
....datetime.datetime(1970, 1, 1, 1, 0)

works super() would be the most likely culprit.

Peter
 
P

peter

Sorry, I didn't pay the necessary attention.

I've only used super() with "normal" methods, but as


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

The docs on super() ( http://docs.python.org/library/functions.html#super
) are a little murky unfortunately.

Cheers,
peter.
 
P

Peter Otten

peter said:
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
 
P

peter

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?

A bit hacky, but does the trick quite nicely otherwise -- thanks :)

Still, I wonder whats up with super(). Obviously I must be missing
something here.

peter.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top