is it a bug ?

Peter Otten __peter__ at web.de
Mon Oct 23 06:47:10 EDT 2006


Sylvain Ferriol wrote:

>>>>>class Toto(float):
>> 
>> ....     eq = float.__eq__
>> ....
>> 
>>>>>Toto().eq(42)
>> 
>> False
>> 
> i can not use it because:
> class toto(float):
> def __init__(self,a=None):pass
> 
> t=toto(a=3)
> TypeError: 'a' is an invalid keyword argument for this function

Override __new__() then:

>>> class Toto(float):
...     def __new__(cls, a=None):
...             if a is None:
...                     a = 42
...             return float.__new__(cls, a)
...     def __init__(cls, a=None):
...             print "random message containing", a
...
>>> Toto()
random message containing None
42.0
>>> Toto(42)
random message containing 42
42.0

Peter



More information about the Python-list mailing list