Return type of operator on inherited class
James Stroud
jstroud at mbi.ucla.edu
Mon Mar 5 07:42:28 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 ?
> I need to override *every* operator I like to use ?
>
You will not need to override if you
s = MyStr(s1 + s2)
James
More information about the Python-list
mailing list