Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #79802

Re: variable attribute name

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'attribute': 0.07; 'string': 0.09; 'attributes': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'denotes': 0.16; 'object()': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:variable': 0.16; 'variable.': 0.16; 'wrote:': 0.18; '(not': 0.18; 'variable': 0.18; 'header:User-Agent:1': 0.23; 'advise.': 0.24; 'mon,': 0.24; 'pass': 0.26; 'code:': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'code': 0.31; 'larry': 0.31; 'class': 0.32; 'but': 0.35; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'first': 0.61; 'you.': 0.62; 'making': 0.63; 'name': 0.63; 'here': 0.66
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ned Batchelder <ned@nedbatchelder.com>
Subject Re: variable attribute name
Date Mon, 27 Oct 2014 15:48:31 -0400
References <6fc1feb3-651e-49e9-8a5d-66dc0599a754@googlegroups.com> <CACwCsY7Sbgb-up5-0us3bYxfcvr-RQ6oQ-m+qDJyaLtAURfrOw@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host 18.189.25.239
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.6.0
In-Reply-To <CACwCsY7Sbgb-up5-0us3bYxfcvr-RQ6oQ-m+qDJyaLtAURfrOw@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.15242.1414439313.18130.python-list@python.org> (permalink)
Lines 24
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1414439313 news.xs4all.nl 2888 [2001:888:2000:d::a6]:41030
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:79802

Show key headers only | View raw


On 10/27/14 2:32 PM, Larry Martell wrote:
> On Mon, Oct 27, 2014 at 2:23 PM, Harvey Greenberg <hjgreenberg@gmail.com> wrote:
>> I want to let the name of an attribute be the string value of a variable.  Here is some code:
>>
>> class Object(object): pass
>> A = Object()
>> s = 'attr'
>> A.<s> = 1
>>
>> The last line denotes the variable value by <s> (not a python form).  What I want is to have A.attr = 1, but 'attr' determined by the value of s.  Please advise.
>
> setattr(A, s, 1)
>

Larry's code will work for you.  If you are making many attributes like 
this, you might be better off just using a dictionary in the first place:

     a = {}
     a[s] = 1


-- 
Ned Batchelder, http://nedbatchelder.com

Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

variable attribute name Harvey Greenberg <hjgreenberg@gmail.com> - 2014-10-27 11:23 -0700
  Re: variable attribute name Larry Martell <larry.martell@gmail.com> - 2014-10-27 14:32 -0400
  Re: variable attribute name Ned Batchelder <ned@nedbatchelder.com> - 2014-10-27 15:48 -0400

csiph-web