Return type of operator on inherited class
Diez B. Roggisch
deets at nospam.web.de
Mon Mar 5 06:55:45 EST 2007
looping wrote:
> Hi,
> my question is on this example:
>
> class MyStr(str):
> def hello(self):
> print 'Hello !'
>
> s1 = MyStr('My string')
> s2 = MyStr('My second string')
>
> s1.hello()
> s2.hello()
>
> s = s1 + s2
>
> s.hello()
>
>>>>
> Hello !
> Hello !
> Traceback (most recent call last):
> File "<string>", line 204, in run_nodebug
> File "<module1>", line 13, in <module>
> AttributeError: 'str' object has no attribute 'hello'
>
> How could I get a type MyStr from the 'plus' (__add__) operation
> without overriding it in my class ?
You can't.
> I need to override *every* operator I like to use ?
Yes.
Diez
More information about the Python-list
mailing list