By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,096 Members | 1,568 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,096 IT Pros & Developers. It's quick & easy.

Add methods to string objects.

P: n/a
Hi all.
I'm writing a simple Python module containing functions to process
strings in various ways. Actually it works importing the module that
contains the function I'm interested in, and calling
my_module.my_function('mystring').

I was just asking if it is possible to "extend" string objects'
behaviour so that it becomes possible to invoke something like
'anystring'.my_method().

1) does the latter approach bring some advantages?
2) how is it possible to achieve this goal?

Any pointer will be appreciated, thanks.
Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
ne*****@gmail.com (Negroup) wrote:
I was just asking if it is possible to "extend" string objects'
behaviour so that it becomes possible to invoke something like
'anystring'.my_method().


You can't quite do that, but you can get close. You can define your own
class which inherits from str, and then create objects of that class. For
example:

class myString (str):
def __init__ (self, value):
self.value = value

def plural (self):
if self.value[-1] in "sz":
return self.value + "es";
else:
return self.value + "s";

foo = myString("foo")
bar = myString("bar")
baz = myString("baz")

print foo.plural(), bar.plural(), baz.plural() # my defined method
print foo.capitalize() # inherited from base class
Jul 19 '05 #2

P: n/a
You can even get closer, but it is NOT recommended

class foostr(str):
def plural (self):
if self.value[-1] in "sz":
return self.value + "es"
else:
return self.value + "s"
#ugly hack
setattr(__builtins__, "str", foostr)

print str("apple").plural()

# this however does not work
# print "apple".plural()

Jul 19 '05 #3

P: n/a
In article <11**********************@z14g2000cwz.googlegroups .com>,
"si*************@gmail.com" <si*************@gmail.com> wrote:
You can even get closer, but it is NOT recommended

class foostr(str):
def plural (self):
if self.value[-1] in "sz":
return self.value + "es"
else:
return self.value + "s"
#ugly hack
setattr(__builtins__, "str", foostr)

print str("apple").plural()

# this however does not work
# print "apple".plural()


It's fascinating that the setattr() works (and I agree with you that it's a
bad idea), but given that it does work, why doesn't it work with a string
literal?
Jul 19 '05 #4

P: n/a
Negroup wrote:
Hi all.
I'm writing a simple Python module containing functions to process
strings in various ways. Actually it works importing the module that
contains the function I'm interested in, and calling
my_module.my_function('mystring').

I was just asking if it is possible to "extend" string objects'
behaviour so that it becomes possible to invoke something like
'anystring'.my_method().


The proper way is to extend the string type by subclassing it:

class S(str):
def my_method(self):
...

Then you can do "S('anystring').my_method()" etc.

Example:
class S(str): .... def lowers(self):
.... return filter(lambda x:x!=x.upper(), self)
.... def uppers(self):
.... return filter(lambda x:x!=x.lower(), self)
.... s = S('Hello World!')
print s.uppers() HW print s.lowers()

elloorld

This means that your additional behaviour isn't available to
plain string literals. You need to instanciate S objects. This
is much less confusing for other programmers who read your code
(or for yourself when you read it a few years from now).
Jul 19 '05 #5

P: n/a
Roy Smith wrote:
In article <11**********************@z14g2000cwz.googlegroups .com>,
"si*************@gmail.com" <si*************@gmail.com> wrote:

You can even get closer, but it is NOT recommended

class foostr(str):
def plural (self):
if self.value[-1] in "sz":
return self.value + "es"
else:
return self.value + "s"
#ugly hack
setattr(__builtins__, "str", foostr)

print str("apple").plural()

# this however does not work
# print "apple".plural()

It's fascinating that the setattr() works (and I agree with you that it's a
bad idea), but given that it does work, why doesn't it work with a string
literal?


Because the string literal is the *actual* C-level builtin string type,
not whatever type happens to be in __builtins__.str at the time. ("At
the time" is also a tricky proposition - string literals are made into
obects at compile time, before __builtins__ is twiddled with.)

BTW, on setattr():

'''
setattr( object, name, value)

This is the counterpart of getattr(). The arguments are an object, a
string and an arbitrary value. The string may name an existing attribute
or a new attribute. The function assigns the value to the attribute,
provided the object allows it. For example, setattr(x, 'foobar', 123) is
equivalent to x.foobar = 123.
'''

i.e. '''setattr(__builtins__, "str", foostr)''' is the same as
'''__builtins__.str = foostr''', but I would agree that the setattr
gives more of a "black magic" warning.
Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.