Return type of operator on inherited class

looping kadeko at gmail.com
Mon Mar 5 06:26:28 EST 2007


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 ?




More information about the Python-list mailing list