Add methods to string objects.
Roy Smith
roy at panix.com
Thu Jun 30 08:48:19 EDT 2005
In article <1120134661.407265.251710 at z14g2000cwz.googlegroups.com>,
"simon.dahlbacka at gmail.com" <simon.dahlbacka at 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?
More information about the Python-list
mailing list