Inheriting str object

Alejandro Barroso iamyourenemy at gmail.com
Tue Feb 6 06:25:08 EST 2007


On 5 feb, 11:48, "kungfoo... at gmail.com" <kungfoo... at gmail.com> wrote:
> 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?

I'm new to this list(this is my first message) and to Python also (I'm
learning these days),  so i'm afraid that this is not what you are
asking for but anyway

you can create an myStr object on the fly, something like:
s=myStr(s.upper())

Alex




More information about the Python-list mailing list