Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'dynamically': 0.07; 'variables': 0.07; 'wednesday,': 0.07; 'collier': 0.09; 'obj': 0.09; 'foo()': 0.16; 'foo(object):': 0.16; 'subject:object': 0.16; 'subject:variable': 0.16; 'utc,': 0.16; '\xa0you': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'all,': 0.19; '>>>': 0.22; 'email addr:gmail.com>': 0.22; '>>>': 0.24; 'instance,': 0.24; 'sorry,': 0.24; 'fairly': 0.24; '>': 0.26; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'object.': 0.31; 'class': 0.32; "i'd": 0.34; 'could': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'skip:& 10': 0.38; 'ben': 0.38; 'to:addr:python-list': 0.38; 'skip:& 20': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'how': 0.40; 'march': 0.61; 'mar': 0.68; '26,': 0.68; 'below:': 0.68; 'subject': 0.69 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=fJ5AZX5LHJlm1W+kCmUIdmHblBPqv01STAOKBxyeVgw=; b=T0daX7uTXqH94bJnFNA14NbdGDGFzKgAI1ZausLMaCTSvh9+5MqLvqkTlAfx2zuibJ tHh1temGmmHyAz5ipF+XEJNlsoeYkc4qa5ogS3wLkW+Py7hIMu6rFNTZSzKT+R2KR3p4 y+v9nLC/g5qikqGZuCkagF+eMFF04IndYuutoN3oHfcY2aI0hcAuf2o5Y0k08FWQEjxN aqHlUfEwGwQNQBNv7/1QYESMVJ6iDvXIo/JQ9jXCG/4nre8S2zesrz3ytXQLTos3b7p2 tCu3fw0KFGtx5uGVJljNeiUJwRkR2iAfLCnmqGqttbOVp5BUbYGVYSY5AEWPmtlfziJE 0N9A== X-Received: by 10.68.110.165 with SMTP id ib5mr88932270pbb.61.1395868783778; Wed, 26 Mar 2014 14:19:43 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Wed, 26 Mar 2014 15:19:03 -0600 Subject: Re: Dynamically reference variable in object To: Python Content-Type: multipart/alternative; boundary=047d7b6769b602a1e604f589077c 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: 57 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1395868786 news.xs4all.nl 2884 [2001:888:2000:d::a6]:55120 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:69143 --047d7b6769b602a1e604f589077c Content-Type: text/plain; charset=ISO-8859-1 On Mar 26, 2014 5:48 AM, "Ben Collier" wrote: > > Sorry, subject was wrong. Please see below: > > On Wednesday, 26 March 2014 11:43:49 UTC, Ben Collier wrote: > > Hi all, > > I know that I can dynamically reference a variable with locals()["i"], for instance, but I'd like to know how to do this with a variable in an object. > > If I have an object called "device", with variables called attr1, attr2 .. attr50, how could I dynamically reference these? > > It's fairly academic, but I'd like to avoid code duplication. You want to access object "attributes", not "variables". You can do this using the functions getattr and setattr like so: >>> class Foo(object): pass ... >>> obj = Foo() >>> setattr(obj, "x", 42) >>> print(obj.x) 42 >>> print(getattr(obj, "x")) 42 --047d7b6769b602a1e604f589077c Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
On Mar 26, 2014 5:48 AM, "Ben Collier&= quot; <bmcollier@gmail.com>= ; wrote:
>
> Sorry, subject was wrong. Please see below:
>= ;
> On Wednesday, 26 March 2014 11:43:49 UTC, Ben Collier =A0wrote:
>= ; > Hi all,
> > I know that I can dynamically reference a varia= ble with locals()["i"], for instance, but I'd like to know ho= w to do this with a variable in an object.
> > If I have an object called "device", with variables cal= led attr1, attr2 .. attr50, how could I dynamically reference these?
>= ; > It's fairly academic, but I'd like to avoid code duplication= .

You want to access object "attributes", not "variables&q= uot;. =A0You can do this using the functions getattr and setattr like so:
>>> class Foo(object): pass
...
>>> obj= =3D Foo()
>>> setattr(obj, "x", 42)
>>> print(ob= j.x)
42
>>> print(getattr(obj, "x"))
42
--047d7b6769b602a1e604f589077c--