Inheriting str object

kungfoobar at gmail.com kungfoobar at gmail.com
Mon Feb 5 05:48:08 EST 2007


I want to have a str with custom methods, but I have this problem:

class myStr(str):
    def hello(self):
        return 'hello '+self

s=myStr('world')
print s.hello() # prints 'hello world'
s=s.upper()
print s.hello() # expected to print 'hello WORLD', but s is no longer
myStr, it's a regular str!

What can I do?




More information about the Python-list mailing list