Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > sub-classing the types in the builtin module datetime

Reply
Thread Tools

sub-classing the types in the builtin module datetime

 
 
Colin J. Williams
Guest
Posts: n/a
 
      08-15-2007
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.

 
Reply With Quote
 
 
 
 
Michael Amrhein
Guest
Posts: n/a
 
      08-15-2007
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
 
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
subtle error slows code by 10x (builtin sum()) - replace builtin sumwithout using import? bdb112 Python 2 07-02-2011 03:13 AM
Re: sub-classing the types in the builtin module datetime Colin J. Williams Python 5 08-17-2007 09:32 PM
mx.DateTime to datetime.datetime mp Python 1 07-28-2006 10:57 PM
datetime: .datetime-.datetime = .timedelta, .time-.time=TypeError ? Christos TZOTZIOY Georgiou Python 3 09-13-2003 10:44 AM
RE: datetime: .datetime-.datetime = .timedelta, .time-.time=TypeError ? Tim Peters Python 0 09-09-2003 12:57 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57