Inheriting str object

K

kungfoobar

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?
 
M

Marc 'BlackJack' Rintsch

In <[email protected]>,
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?

Return a `myStr` instance instead of a regular `str`:

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

Ciao,
Marc 'BlackJack' Rintsch
 
B

Benjamin Niemann

Marc said:
In <[email protected]>,


Return a `myStr` instance instead of a regular `str`:

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

yes, but the 'upper' method is the problem here.
So you'd have to override all string methods, like

class myStr(str):
...

def upper(self):
return myStr(str.upper(self))


And I'm not sure, if it then works in the intended way...
What you are probably looking for, is to extend the 'str' class itself, so
every str instance has your added functionality.
Don't know, if this is possible at all, but usually it's not a good idea to
mess with the bowels of Python unless you have some greater surgical
skills.


HTH
 
V

Virgil Dupras

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?

To prevent operations with your myStr class to return a simple str
instance, you will have to override pretty much all str method, as
well as magic methods, such as __add__, __radd__ etc.. Major pain in
perspective. You might want to reconsider you developing strategy.
 
?

=?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=

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!

You could use the proxy pattern:

class GreeterString(object):
def __init__(self, str):
self.proxy = str

def hello(self):
return 'hello ' + self.proxy

def __getattr__(self, attr):
if attr in dir(self.proxy):
proxy_attr = getattr(self.proxy, attr)
if callable(proxy_attr):
def wrapper(*args, **kwargs):
return self.__class__(proxy_attr())
return wrapper

def __str__(self):
return self.proxy.__str__()


gs = GreeterString('world')
print gs.upper().hello()

Magic methods has to be overridden manually, I think.
 
A

Alejandro Barroso

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top