0

This has to be asked somewhere already, but I don't know the right words to find or frame it. So please bear with me.

This code fragment:

code = 'VegCode'

d = {}
codes = foo_func

for item in codes:
    print item.code

Results in:

RuntimeError: Row: Field code does not exist    

if I change it like this, it will work, but I don't want to hard code the variable name:

for item in codes:
    print item.VegCode

How do I pass the value of code to the item object instead of it's name? (and please tell me what key words would have led to an answer!)

matt wilkie
  • 17,268
  • 24
  • 80
  • 115

1 Answers1

1

Use getattr: getattr(item, code)

Keywords: how to access object's attribute by its name

Artem Volkhin
  • 1,362
  • 8
  • 22