Subclassing datetime.date
John Bokma
john at castleamber.com
Sat Feb 6 16:42:29 EST 2010
Sir Wilhelm the Sturdy <wgaggioli at gmail.com> writes:
F> Hi all,
>
> I recently attempted to subclass the datetime.date object resulting in
> horror and confusion, before submitting to a has-a relationship.
> That's all fine and dandy, but out of curiosity I'd like to know what
> I'm missing.
>
> I was attempting to allow more flexible instancing of an object, like
> so:
>
> import datetime
>
> class MyDate(datetime.date):
>
> def __init__(self,*args,**kw):
>
> if len(kw) + len(args) > 1:
> self.construct(*args,**kw)
>
> def construct(self,d,m=None,y=None,**kw):
>
> today = datetime.date.today()
> if m is None:
> m = today.month
> if y is None:
> y = today.year
>
> datetime.date.__init__(self,y,m,d,**kw)
>
>
> However, it wasn't having the desired effect. Indeed, I could still
> only instance it with 3 variables lest I get errors, and when I did
> call it with 3 variables it didn't reflect the order change I had
> implemented. Indeed, the properties were already set before it even
> got to the construct method.
__init__ is *not* the construction method, __new__ is (at least with new
style classes)
> Is there some kind of built in I'm missing here?
I guess __new__ but I am quite the newbie.
--
John Bokma j3b
Hacking & Hiking in Mexico - http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development
More information about the Python-list
mailing list