sub-classing the types in the builtin module datetime

Discussion in 'Python' started by Colin J. Williams, Aug 15, 2007.

  1. I wish to sub-class (if that's the right word) datetime and to use a
    different signature for the constructor.

    The second part has gone smoothly, but it is difficult to access the
    type's methods from the sub-class instance.

    I'm beginning to wonder whether it might might be simpler to write my
    own Date class.

    Does anyone have any comments please?

    Colin W.
    Colin J. Williams, Aug 15, 2007
    #1
    1. Advertising

  2. Colin J. Williams wrote:
    > I wish to sub-class (if that's the right word) datetime and to use a
    > different signature for the constructor.
    >
    > The second part has gone smoothly, but it is difficult to access the
    > type's methods from the sub-class instance.
    >

    What's difficult?
    >>> from datetime import datetime
    >>> class mydt(datetime): pass

    ....
    >>> dt=mydt(2007,8,15)
    >>> dt.day

    15
    >>> dt.now()

    mydt(2007, 8, 15, 18, 57, 58, 562000)

    How did you overwrite the constuctor?
    Are you sure your constructor really returns a subclass of datetime?

    > I'm beginning to wonder whether it might might be simpler to write my
    > own Date class.
    >

    If you only want to change the signature of the constuctor, just write a
    factory function.

    > Does anyone have any comments please?
    >
    > Colin W.
    >


    Have fun,
    Michael
    Michael Amrhein, Aug 15, 2007
    #2
    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. Pritam Kamat

    Obfuscation and sub classing

    Pritam Kamat, Aug 8, 2003, in forum: Java
    Replies:
    2
    Views:
    520
    pete kirkham
    Aug 8, 2003
  2. Colin J. Williams

    Sub-classing NumArray - two questions

    Colin J. Williams, Oct 1, 2003, in forum: Python
    Replies:
    0
    Views:
    271
    Colin J. Williams
    Oct 1, 2003
  3. Bernard Lebel

    Problem with sub-classing

    Bernard Lebel, Jul 17, 2006, in forum: Python
    Replies:
    3
    Views:
    362
    Peter Otten
    Jul 17, 2006
  4. Colin J. Williams
    Replies:
    5
    Views:
    314
    Colin J. Williams
    Aug 17, 2007
  5. Colin J. Williams

    sub-classing datetime

    Colin J. Williams, Feb 7, 2013, in forum: Python
    Replies:
    1
    Views:
    108
    marduk
    Feb 7, 2013
Loading...

Share This Page