[Tutor] Poorly understood error involving class inheritance

Serdar Tumgoren zstumgoren at gmail.com
Thu Sep 10 22:42:04 CEST 2009


>>>> class dummy2(str):
> ...     def __init__(self,dur=0):
> ...             self.dur=dur
> ...
>>>> z=dummy2(3)
>>>> z.dur
> 3
>
> So far so good.  But:
>
>>>> z=dummy2(dur=3)
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> TypeError: 'dur' is an invalid keyword argument for this function
>>>>

I think you're problem may stem from the fact that you subclassed the
string type and then tried to pass in an integer.

>>> class Dummy2(int):
...     def __init__(self, dur=0):
...         self.dur = dur
...
>>> z = Dummy2(3)
>>> z.dur
3

When you sub "int" for "str", it seems to work. Is there a reason
you're not just subclassing "object"? I believe doing so would give
you the best of both worlds.


More information about the Tutor mailing list