[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Possible to tack on random stuff to objects?

AdamB

2/12/2008 5:03:00 AM

Is it possible to tack on arbitrary attributes to a python object?
For example:

s = 'nice 2 meet you'
s.isFriendly = True

In the above example Python complains on the second line with:

AttributeError: 'str' object has no attribute 'isFriendly'

Is there another way?
4 Answers

Bruno Desthuilliers

2/12/2008 9:24:00 AM

0

Cruxic a écrit :
> Is it possible to tack on arbitrary attributes to a python object?

Depends on the object's class. In the common case it's possible but
there are a couple cases - mostly builtin immutable types - where it's not.

> For example:
>
> s = 'nice 2 meet you'
> s.isFriendly = True
>
> In the above example Python complains on the second line with:
>
> AttributeError: 'str' object has no attribute 'isFriendly'
>
> Is there another way?

>>> class MyString(str): pass
....
>>> s = MyString("hello")
>>> s.is_friendly = True
>>> s
'hello'
>>>

AdamB

2/13/2008 2:41:00 AM

0

That does the trick. Thanks, Bruno.

On Feb 12, 1:23 am, Bruno Desthuilliers <bruno.
42.desthuilli...@wtf.websiteburo.oops.com> wrote:
> Cruxic a écrit :
>
> > Is it possible to tack on arbitrary attributes to a python object?
>
> Depends on the object's class. In the common case it's possible but
> there are a couple cases - mostly builtin immutable types - where it's not.
>
> > For example:
>
> > s = 'nice 2 meet you'
> > s.isFriendly = True
>
> > In the above example Python complains on the second line with:
>
> > AttributeError: 'str' object has no attribute 'isFriendly'
>
> > Is there another way?
>
> >>> class MyString(str): pass
> ...
> >>> s = MyString("hello")
> >>> s.is_friendly = True
> >>> s
> 'hello'
> >>>

marcus

10/16/2011 6:31:00 PM

0

On Oct 16, 9:08 am, topazgalaxy <topazgal...@gmail.com> wrote:
>
> > >I think all this fighting is rather silly.
>
> > It goes on everywhere - all over Usenet and in every group.
>
> Yes,  you are correct. Sad but true.
> In a way  this group  is a bit like our Congress.

Unfortunately, without term limits.

topaz

10/16/2011 6:35:00 PM

0

On Oct 16, 2:25 pm, Mack A. Damia <mybaconbu...@hotmail.com> wrote:
> On Sun, 16 Oct 2011 06:08:34 -0700 (PDT), topazgalaxy
>
>
>
>
>
> <topazgal...@gmail.com> wrote:
> >On Oct 15, 3:44 pm, Mack A. Damia  <mybaconbu...@hotmail.com> wrote:
> >> On Sat, 15 Oct 2011 10:46:27 -0700 (PDT), topazgalaxy
>
> >> <topazgal...@gmail.com> wrote:
> >> >On Oct 14, 6:09 pm, "RichL" <rpleav...@yahoo.com> wrote:
> >> >> "moonpie" <mr_rc_moon...@yahoo.com> wrote in message
>
> >> >>news:vbdg97pa76bgs0ac19jiq19p5hkdcs3h9i@4ax.com...
>
> >> >> > On Thu, 13 Oct 2011 22:36:34 -0400, "RichL" <rpleav...@yahoo.com>
> >> >> > wrote:
>
> >> >> >>"moonpie" <mr_rc_moon...@yahoo.com> wrote in message
> >> >> >>news:jhpd979get1ug2peoujd9kmqcrmbudo9g9@4ax.com...
> >> >> >>> On Wed, 12 Oct 2011 19:05:10 -0400, "RichL" <rpleav...@yahoo.com>
> >> >> >>> wrote:
>
> >> >> >>>>"moonpie" <mr_rc_moon...@yahoo.com> wrote in message
> >> >> >>>>news:gv4b97tqhfuh1lt4ghbdkag6dfmi556uq2@4ax.com...
> >> >> >>>>> On Tue, 11 Oct 2011 19:43:19 -0400, "RichL" <rpleav...@yahoo.com>
> >> >> >>>>> wrote:
>
> >> >> >>>>>>"moonpie" <mr_rc_moon...@yahoo.com> wrote in message
> >> >> >>>>>>news:g2g897tg4gg1l5t9hq94pkla9og9jdtcgh@4ax.com...
>
> >> >> >>>>>>> exactly, and that idea right there, is whats totally askew in the
> >> >> >>>>>>> liberal I HATE THE RICH definition of what "corporate america" is.
>
> >> >> >>>>>>You know, you get all bent out of shape when people stereotype
> >> >> >>>>>>conservatives.  I suspect you'd get even more bent out of shape if
> >> >> >>>>>>someone
> >> >> >>>>>>came along and said "white southerners are bigots".
>
> >> >> >>>>>>So maybe you shouldn't rely on stereotypes to characterize people you
> >> >> >>>>>>disagree with.
>
> >> >> >>>>> or maybe I should simply start to insult them when they disagree with
> >> >> >>>>> me, like the way Blackmonk, Mac, and Ray do.
>
> >> >> >>>>> maybe if they wouldnt keep displaying all the same old liberal
> >> >> >>>>> stereotypes, it wouldnt make sense to keep calling them on it.
>
> >> >> >>>>> are you blind to the pattern of I HATE THE RICH that continually
> >> >> >>>>> manifests itself right here on this newsgroup? I see it over and over.
>
> >> >> >>>>I haven't actually seen anyone say "I hate the rich".
>
> >> >> >>> well, you're not reading very closely then. I guess you arent paying
> >> >> >>> any attention to Macs diatribes, or Rays recent hate-rant
>
> >> >> >>Mac's in my killfile.  I didn't see Ray say he hates the rich, but I did
> >> >> >>see
> >> >> >>where he says he earns a lot of money so in a sense he's rich, and I also
> >> >> >>saw where he said he went to business school.
>
> >> >> >>> look, you can stop lecturing me Rich. You were happy as a clam to join
> >> >> >>> in on Zontars "southern bashing"
>
> >> >> >>I'm calling you on that one, bub.  Post the link or shut the fuck up.
>
> >> >> > how sweet.
>
> >> >> > you really are a prince aint ya.
>
> >> >> > happy friday and have a nice weekend.
>
> >> >> And there it is, folks.  Fabricated accusation, asked to provide evidence (I
> >> >> provided evidence to the contrary), he doesn't address it at all.  Just
> >> >> pretend it never happened.
>
> >> >> Enough is enough.  Bye!- Hide quoted text -
>
> >> >> - Show quoted text -
>
> >> >I think all this fighting is rather silly.
>
> >> It goes on everywhere - all over Usenet and in every group.
>
> >Yes,  you are correct. Sad but true.
> >In a way  this group  is a bit like our Congress.
>
> A bit like our nation.  Usenet is only a reflection of society.  We
> just don't stop strangers in malls and on the streets and argue with
> them about whether or not Yoko broke up The Beatles.- Hide quoted text -
>
> - Show quoted text -

.......or write sentimental poems focusing on Yoko's breasts as M C
Hammered does and share them with the world.

(no criticism Mr Charlie, just a little humor here).

More seriously, I think that the state of being anonymous for many
people allows them to feel more free to say what is on their mind,
for better or worse. When any of us is in the mall, we are facing
another person or persons so it is a bit different.
I do agree that Usernet is a reflection of society, but perhaps, it is
a reflection of the society that is literate, and educated enough to
use and possibly own a computer.
There are still folks out there, around the world who have never
touched a computer.