Re: Add methods to string objects.
- From: Roy Smith <roy@xxxxxxxxx>
- Date: Thu, 30 Jun 2005 08:22:07 -0400
negroup@xxxxxxxxx (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
.
- Follow-Ups:
- Re: Add methods to string objects.
- From: simon.dahlbacka@xxxxxxxxx
- Re: Add methods to string objects.
- References:
- Add methods to string objects.
- From: Negroup
- Add methods to string objects.
- Prev by Date: Re: How to compare two directories?
- Next by Date: Re: POP3 and "seen" flag
- Previous by thread: Add methods to string objects.
- Next by thread: Re: Add methods to string objects.
- Index(es):
Relevant Pages
|