Reputation: 2050
I've looked everywhere for this but haven't found a suitable answer yet and I don't want to use a dictionary.
How can I convert a string to a variable name? For example I have this call:
object.variable
This variable
is defined in the object
. However I have a LOT of variables (for a reason) and more to come. It seems stupid to do this:
s = 'variable'
if s == 'variable': object.variable
elif s == 'variable2: object.variable2
etc....
So is there a way to convert a string to a variable name?
Upvotes: 0
Views: 369
Reputation: 133664
getattr(object, variable)
However I don't see any good reason to be accessing attributes like this...
Upvotes: 1