Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   sub-classing the types in the builtin module datetime (http://www.velocityreviews.com/forums/t529472-sub-classing-the-types-in-the-builtin-module-datetime.html)

Colin J. Williams 08-15-2007 04:04 PM

sub-classing the types in the builtin module datetime
 
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.


Michael Amrhein 08-15-2007 05:03 PM

Re: sub-classing the types in the builtin module datetime
 
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


All times are GMT. The time now is 09:21 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.