Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Create attribute from string

0 views
Skip to first unread message

Chris Rebert

unread,
Dec 31, 2009, 1:50:38 AM12/31/09
to AON LAZIO, pytho...@python.org
On Wed, Dec 30, 2009 at 9:22 PM, AON LAZIO <aonl...@gmail.com> wrote:
> Hi,
>     I would like to know how we could create attribute from string
>
> say I want to assign value 0.05 to an object attribute
>
> I like to input "SIGNIFICANT" and 0.05 and get
> object.SIGFICANT equals to 0.05

setattr(obj, "SIGNIFICANT", 0.05)

Cheers,
Chris
--
http://blog.rebertia.com

Steve Holden

unread,
Dec 31, 2009, 8:10:33 AM12/31/09
to pytho...@python.org
Chris Rebert wrote:
> On Wed, Dec 30, 2009 at 9:22 PM, AON LAZIO <aonl...@gmail.com> wrote:
>> Hi,
>> I would like to know how we could create attribute from string
>>
>> say I want to assign value 0.05 to an object attribute
>>
>> I like to input "SIGNIFICANT" and 0.05 and get
>> object.SIGFICANT equals to 0.05
>
> setattr(obj, "SIGNIFICANT", 0.05)
>
This bald reply, however, hides the simple fact that this is ALMOST
NEVER what you actually want to do. Look at using a dict to store these
values against string keys rather than playing with the namespace in
this way.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/

0 new messages