More puzzling behavior while subclassing datetime

insyte at insyte at
Thu Nov 16 07:38:24 CET 2006

With assistance from Gabriel and Frederik (and a few old threads in
c.l.p.) I've been making headway on my specialized datetime class.  Now
I'm puzzled by behavior I didn't expect while attempting to use some of
the alternate datetime constructors.  Specifically, it appears if I
call it calls the __new__ method of my class but
treats keyword arguments as if they were positional.

My class:

class GeneralizedTime(datetime):
    def __new__(cls, time=None, *args, **kwargs):
        print time, args, kwargs
        if isinstance(time, str):
            timeValues, tzOffset = cls.stringToTimeTuple(time)
            return datetime.__new__(cls, tzinfo=GenericTZ(tzOffset),
        elif isinstance(time, datetime):
            timeValues = time.timetuple()[:6]
            tzOffset = time.utcoffset()
            return datetime.__new__(cls, tzinfo=GenericTZ(tzOffset),
        elif time is None:
            print "Still gotta figure out now to do this one..."
            raise Invalidtime(time)
    def stringToTimeTuple(timeString):
         ... regex that parses timeString ...

2006 (11, 16, 0, 35, 18, 747275, None) {}
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "", line 106, in __new__
     raise InvalidTime(time)
gentime.InvalidTime: 2006

So it appears the time tuple is being passed to
GeneralizedTime.__new__, but the first value is being assigned to the
"time" argument.

Is this a side effect of how datetime is implemented?  Or am I doing
something screwy?



More information about the Python-list mailing list