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: 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 Subject: Re: variable attribute name Date: Mon, 27 Oct 2014 15:48:31 -0400 References: <6fc1feb3-651e-49e9-8a5d-66dc0599a754@googlegroups.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: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On 10/27/14 2:32 PM, Larry Martell wrote: > On Mon, Oct 27, 2014 at 2:23 PM, Harvey Greenberg 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. = 1 >> >> The last line denotes the variable value by (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