At Wednesday 15/11/2006 19:30, [EMAIL PROTECTED] wrote:

I am writing a class that subclasses datetime.datetime in order to add
a few specialized methods.  So far the __init__ looks like this:

class myDateTime(datetime.datetime):
    def __init__(self, time, *args, **kwargs):
        if isinstance(time, str):
            timeTuple, tzOffset = self.magicMethod(timeStr)
            datetime.__init__(self, tzinfo=GenericTZ(tzoffset),
**timeTuple)

(I assume you mean *timeTuple)

I would also like to pass in instances of datetime.datetime and have my
class wrap it in the new interface.  Something like this:

mdt = myDateTime(datetime.datetime.now())

I suppose I could do something like this:

    elif isinstance(time, datetime.datetime):
        timetuple = time.timetuple()
        tzoffset = time.utcoffset()
        datetime.__init__(self, tzinfo=GenericTZ(tzoffset),
**timetuple)

However, that feels rather... awkward.  Is there a better/cleaner way?
Perhaps a way to directly wrap my new interface around the passed-in
datetime.datetime instance?

I don't see any other suitable constructor for datetime; perhaps one taking a datetime instance would be useful here. Each individual component (year, month, etc.) is stored by itself, so timetuple() isn't complex; apart from the overhead of constructing a new object, you get an unneeded extra check of validity.

Another way would be monkey-patching the datetime class adding your own methods. This way the new methods would be available on any datetime instance - you don't need myDateTime class. You can move "magicmethod" into a factory function used to construct datetime objects.


--
Gabriel Genellina
Softlab SRL
__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis! ¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to