Re: Add methods to string objects.
- From: Roy Smith <roy@xxxxxxxxx>
- Date: Thu, 30 Jun 2005 08:48:19 -0400
In article <1120134661.407265.251710@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
"simon.dahlbacka@xxxxxxxxx" <simon.dahlbacka@xxxxxxxxx> 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?
.
- Follow-Ups:
- Re: Add methods to string objects.
- From: Rocco Moretti
- Re: Add methods to string objects.
- References:
- Add methods to string objects.
- From: Negroup
- Re: Add methods to string objects.
- From: Roy Smith
- Re: Add methods to string objects.
- From: simon.dahlbacka@xxxxxxxxx
- Add methods to string objects.
- Prev by Date: Re: map vs. list-comprehension
- Next by Date: Re: Store multiple dictionaries in a file
- Previous by thread: Re: Add methods to string objects.
- Next by thread: Re: Add methods to string objects.
- Index(es):
Relevant Pages
|