Subclassing datetime.date

Discussion in 'Python' started by Sir Wilhelm the Sturdy, Feb 6, 2010.

  1. 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
    Sir Wilhelm the Sturdy, Feb 6, 2010
    #1
    1. Advertising

  2. Sir Wilhelm the Sturdy

    John Bokma Guest

    Sir Wilhelm the Sturdy <> 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
    John Bokma, Feb 6, 2010
    #2
    1. Advertising

  3. En Sat, 06 Feb 2010 18:42:29 -0300, John Bokma <>
    escribió:

    > Sir Wilhelm the Sturdy <> 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
    Gabriel Genellina, Feb 7, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christos TZOTZIOY Georgiou
    Replies:
    3
    Views:
    704
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  2. Tim Peters
    Replies:
    0
    Views:
    532
    Tim Peters
    Sep 9, 2003
  3. mp
    Replies:
    1
    Views:
    386
    John Machin
    Jul 28, 2006
  4. Mike Rooney
    Replies:
    1
    Views:
    288
  5. Martin
    Replies:
    0
    Views:
    333
    Martin
    Dec 27, 2008
Loading...

Share This Page