Overloading ctor doesn't work?

Steve Holden steve at holdenweb.com
Thu Jan 20 13:45:56 EST 2005


Martin Häcker wrote:

> Hi there,
> 
> I just tried to run this code and failed miserably - though I dunno why. 
> Could any of you please enlighten me why this doesn't work?
> 
> Thanks a bunch.
> 
> --- snip ---
> import unittest
> from datetime import datetime
> 
> class time (datetime):
>   def __init__(self, hours=0, minutes=0, seconds=0, microseconds=0):
>     print "blah"
>     datetime.__init__(self, 1, 1, 1, hours, \
>     minutes, seconds, microseconds)
> 
> 
> class Test (unittest.TestCase):
>   def testSmoke(self):
>     # print time() # bombs, and complains that
>     # the time ctor needs at least 3 arguments
>     self.assertEquals(datetime(1,1,1,1,2,3,4),time(1,2,3,4))
> 
> 
> if __name__ == '__main__':
>   unittest.main()
> --- snap ---
> 
> The reason I want to do this is that I want to work with times but I 
> want to do arithmetic with them. Therefore I cannot use the provided 
> time directly.
> 
> Now I thought, just overide the ctor of datetime so that year, month and 
>  day are static and everything should work as far as I need it.
> 
> That is, it could work - though I seem to be unable to overide the ctor. :(
> 
> Why is that?
> 
Perhpas the error message or output, which you don't provide, would 
avoid our having to use our psychic powers? ;-)

In other words: what's wrong? How do you *know* you can't override it?

regards
  Steve
-- 
Steve Holden               http://www.holdenweb.com/
Python Web Programming  http://pydish.holdenweb.com/
Holden Web LLC      +1 703 861 4237  +1 800 494 3119



More information about the Python-list mailing list