0

I'm working with Python 2.6 here. Perhaps my title is misleading. So I will explain here. I tried my best to search for a solution, but since I'm rather new I have no clue what to search.

Essentially, I'm getting an input string value from the user, and I wish to do the following:

Let's say

x=['reg1','reg2','reg3']

I want to write:

for reg in x:
    display_value.reg

Such that python reads the following for every iteration:

display_value.regX

and NOT:

display_value.'regX'
Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
RRR
  • 69
  • 6
  • wait... changing a string to a variable? What does that make a string? anyway, your question refers to using `getattr()`. You want `getattr(display_value, reg)` – corvid Jun 19 '14 at 17:44
  • What advantage do you think this will offer over storing `reg` as a list or a dictionary which could be iterated over directly? Almost always when you're using more than one integer suffix in a variable name you've taken a wrong step. – DSM Jun 19 '14 at 17:44
  • This is the sort of thing I should've been searching for. As I said, I had little idea as to *what* I should search. Thank you very much for this – RRR Jun 19 '14 at 17:44
  • Most likely, you want to make `display_value` a dictionary, and not faff around with `getattr()`. – Russell Borogove Jun 19 '14 at 17:45
  • @crow Like I said, I didn't know how to ask the question. Apologies. getattr is what I was looking for. – RRR Jun 19 '14 at 17:46
  • @DSM what do you mean by iterating over directly? I understand lists and dictionaries – RRR Jun 19 '14 at 17:48
  • not to be harsh, but if you don't know what to ask, it's likely you're not really understanding what you're asking. I think a more appropriate title would be "extracting a string variable from a class" – corvid Jun 19 '14 at 17:48
  • @user3757519: I mean that if you had (effectively) `display_value.reg = [1,2,3]`, you could do `for x in display_value.reg`, etc. Or similarly if it were a dictionary. – DSM Jun 19 '14 at 17:51
  • I plus one'd because I think this sort of question is needed, and it will prevent future duplicates from askers thinking along the same lines. – Russia Must Remove Putin Jun 19 '14 at 17:57

2 Answers2

2

What you want is getattr:

x=['reg1','reg2','reg3']

for attr in x:                   
    getattr(display_value, attr) # same as display_value.attr, attr not a string.

You may find it useful to provide a default value in case your object doesn't have the attr:

foo = getattr(obj, attr, 'default value') # typically better to use None.
Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
1

Although @Aaron Hall's solution works, I'd say that "what you really want is to use a dict, not an object instance".

Although it's possible to introspect almost everything in Python, and thus you can use an object as a dict in Python, full-blown objects carry around some extra baggage and complexity that makes them ill-suited for the simple case of a mapping (e.g. key1 => value1, key2 => value2).

A dict is the appropriate container type for this:

keys = ['reg1','reg2','reg3']
display_value = dict( map(None, keys, ()) )

for key in keys:
    # do something with the value of display_value[key]
Dan Lenski
  • 76,929
  • 13
  • 76
  • 124