3

I'm not sure the best way to ask this, so I'm going to try by example. Is there an easy way in Python to accomplish the following?

instead of referencing an object like this:

>> print myobject.someattrib
5

...use an expression which will be evaluated first, and then dereferenced?:

>> obj_name = "someattrib"
>> print myobject.<print value of obj_name>
5

1 Answer 1

7

You can do:

print getattr(myobject, obj_name)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.