Re: Add methods to string objects.



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?
.



Relevant Pages

  • Re: losing poundage
    ... Spacing is closer together, more support for each string. ... Prev by Date: ...
    (rec.sport.tennis)
  • Re: Organizing Constants for Easier Maintenance
    ... Oops, I looked a little closer at your code and the following seems to be ... Function GetCallouts(ByVal sScale As String) As String ... Prev by Date: ...
    (microsoft.public.vb.general.discussion)
  • Re: sortout things from an array
    ... So that means you cant emit your closer right way, ... You have to set a boolean when you start a ... Prev by Date: ...
    (comp.lang.java.help)
  • Re: Pitman CSI Formula
    ... that a randomly produced string will be at least as close or closer to ... the reference than the maximum HD distance! ... really helpful formula you got there Howard;) ...
    (talk.origins)
  • Re: Archtop Intonation Problem - Please Help
    ... closer to the tail, and the Fisrt to 3rd string closer to the neck. ... jazz guitarists used similarly heavy gauges. ... I tried the 'slant' idea, ...
    (rec.music.makers.guitar.jazz)