Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Subclassing datetime.date

Reply
Thread Tools

Subclassing datetime.date

 
 
Sir Wilhelm the Sturdy
Guest
Posts: n/a
 
      02-06-2010
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.

Is there some kind of built in I'm missing here?

Thanks all,
Will
 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      02-06-2010
Sir Wilhelm the Sturdy <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      02-07-2010
En Sat, 06 Feb 2010 18:42:29 -0300, John Bokma <(E-Mail Removed)>
escribió:

> Sir Wilhelm the Sturdy <(E-Mail Removed)> writes:
>>
>> 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.
>>
>> class MyDate(datetime.date):
>>
>> def __init__(self,*args,**kw):
>> ...


> __init__ is *not* the construction method, __new__ is (at least with new
> style classes)


You're right. But in this case, instead of overriding __new__, I would add
a separate constructor to avoid confusion with the existing one (a
classmethod, actually, like fromordinal and fromtimestamp):

py> from datetime import date
py> class date(date):
.... @classmethod
.... def fromDMY(cls, d, m=None, y=None):
.... today = date.today()
.... if m is None: m = today.month
.... if y is None: y = today.year
.... return cls(y, m, d)
....
py> date.fromDMY(20, 3, 2010)
date(2010, 3, 20)
py> date.fromDMY(11)
date(2010, 2, 11)

--
Gabriel Genellina

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thread safety when subclassing the Page class thechaosengine ASP .Net 2 12-10-2004 02:48 PM
Subclassing Controls TheStripe ASP .Net 2 09-27-2004 01:17 PM
SubClassing a TextBox Felbrigg ASP .Net 0 04-27-2004 09:05 AM
Subclassing from System.Web.UI.Page - Designer Error =?Utf-8?B?Q2h1Y2sgSGFlYmVybGU=?= ASP .Net 1 02-20-2004 05:05 PM
Subclassing UserControl makes the Visual Studio designer does not work S Guiboud ASP .Net 1 07-18-2003 03:55 PM



Advertisments