Overloading ctor doesn't work?
francis.girard at free.fr
Thu Jan 20 20:00:36 CET 2005
It looks like the assertEquals use the != operator which had not been defined
to compare instances of your time class and instances of the datetime class.
In such a case, the operator ends up in comparing the references to instances,
i.e. the "id" of the objects, i.e. their physical memory addresses, which of
course can't be the same. And that is why your test fails.
Consider defining the __cmp__ method (see 2.3.3 Comparisons of the library
reference). Sess also the operator module.
Le jeudi 20 Janvier 2005 19:23, Martin Häcker a écrit :
> 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
> if __name__ == '__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?
> cu Martin
> Reach me at spamfaenger (at) gmx (dot) net
More information about the Python-list