Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #69143
| 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 | <ian.g.kelly@gmail.com> |
| 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 | <cddebac0-8d5d-406f-8712-a9ce806a748d@googlegroups.com> |
| References | <d9daaeb5-04ef-43e8-b60e-3c4ba5790a9d@googlegroups.com> <cddebac0-8d5d-406f-8712-a9ce806a748d@googlegroups.com> |
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | Wed, 26 Mar 2014 15:19:03 -0600 |
| Subject | Re: Dynamically reference variable in object |
| To | Python <python-list@python.org> |
| 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 <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.8586.1395868786.18130.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
[Multipart message — attachments visible in raw view] - view raw
On Mar 26, 2014 5:48 AM, "Ben Collier" <bmcollier@gmail.com> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Dynamically reference member of array Ben Collier <bmcollier@gmail.com> - 2014-03-26 04:43 -0700
Dynamically reference variable in object Ben Collier <bmcollier@gmail.com> - 2014-03-26 04:44 -0700
Re: Dynamically reference variable in object Ian Kelly <ian.g.kelly@gmail.com> - 2014-03-26 15:19 -0600
Re: Dynamically reference variable in object Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-26 23:42 +0000
Re: Dynamically reference member of array "R. Michael Weylandt" <michael.weylandt@gmail.com> - 2014-03-29 22:50 -0400
csiph-web