I want to know how i can concat object fields with a variable, it's hard to explain to me, let me give an example
Example:
My object have:
myobject.name = 'Red'
myobject.lastname = 'Foo'
and I have a function like this:
my function
..........
some_dumb_field = name
print myobject.some_dumb_field
And this crashes!, how can i concat the str of the field like an object?
I'm guessing you are looking for getattr:
print getattr(myobject, some_dumb_field)
This will look up the attribute of myobject
whose name is given by the string some_dumb_field
.
For example,
getattr(myobject, 'name')
is equivalent to
myobject.name
(Warning: This could be called attribute lookup; you won't find it under the term "concat".)
来源:https://stackoverflow.com/questions/23517521/attribute-lookup-str-and-objects-like-object-myvar