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


Groups > comp.lang.python > #69365

Re: Dynamically reference member of array

References <d9daaeb5-04ef-43e8-b60e-3c4ba5790a9d@googlegroups.com>
From "R. Michael Weylandt" <michael.weylandt@gmail.com>
Date 2014-03-29 22:50 -0400
Subject Re: Dynamically reference member of array
Newsgroups comp.lang.python
Message-ID <mailman.8710.1396147859.18130.python-list@python.org> (permalink)

Show all headers | View raw


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

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


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