Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #69116 > unrolled thread
| Started by | Ben Collier <bmcollier@gmail.com> |
|---|---|
| First post | 2014-03-26 04:43 -0700 |
| Last post | 2014-03-29 22:50 -0400 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.python
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
| From | Ben Collier <bmcollier@gmail.com> |
|---|---|
| Date | 2014-03-26 04:43 -0700 |
| Subject | Dynamically reference member of array |
| Message-ID | <d9daaeb5-04ef-43e8-b60e-3c4ba5790a9d@googlegroups.com> |
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. Thanks, Ben
[toc] | [next] | [standalone]
| From | Ben Collier <bmcollier@gmail.com> |
|---|---|
| Date | 2014-03-26 04:44 -0700 |
| Subject | Dynamically reference variable in object |
| Message-ID | <cddebac0-8d5d-406f-8712-a9ce806a748d@googlegroups.com> |
| In reply to | #69116 |
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. > > > > Thanks, > > > > Ben
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2014-03-26 15:19 -0600 |
| Subject | Re: Dynamically reference variable in object |
| Message-ID | <mailman.8586.1395868786.18130.python-list@python.org> |
| In reply to | #69117 |
[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
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2014-03-26 23:42 +0000 |
| Subject | Re: Dynamically reference variable in object |
| Message-ID | <533365de$0$29994$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #69143 |
On Wed, 26 Mar 2014 15:19:03 -0600, Ian Kelly wrote: > You want to access object "attributes", not "variables". In fairness to the OP, the terminology "instance variables" meaning attributes or members of an instance is (sadly, in my opinion) common in some other languages, such as Java, and occasionally creeps into even the Python docs. I've often ranted about this, and won't repeat it now, only point out that the preferred and most common terminology in Python circles is "attribute", or "instance attribute" if you need to distinguish it from "class attribute". -- Steven D'Aprano http://import-that.dreamwidth.org/
[toc] | [prev] | [next] | [standalone]
| From | "R. Michael Weylandt" <michael.weylandt@gmail.com> |
|---|---|
| Date | 2014-03-29 22:50 -0400 |
| Message-ID | <mailman.8710.1396147859.18130.python-list@python.org> |
| In reply to | #69116 |
On Wed, Mar 26, 2014 at 7:43 AM, Ben Collier <bmcollier@gmail.com> 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? > I think the pythonic term you're looking for is "attribute". See getattr() and setattr() Cheers, Michael
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web