Re: Add methods to string objects.

2005-06-30 Thread Rocco Moretti
Roy Smith wrote: > In article <[EMAIL PROTECTED]>, > "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> 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" >>els

Re: Add methods to string objects.

2005-06-30 Thread Magnus Lycka
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 t

Re: Add methods to string objects.

2005-06-30 Thread Roy Smith
In article <[EMAIL PROTECTED]>, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> 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 se

Re: Add methods to string objects.

2005-06-30 Thread [EMAIL PROTECTED]
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() #

Re: Add methods to string objects.

2005-06-30 Thread Roy Smith
[EMAIL PROTECTED] (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,