How do I convert a string to the variable name in Python?
For example, if the program contains a object named self.post
that contains a variable named, I want to do something like:
somefunction("self.post.id") = |Value of self.post.id|
Advertisement
Answer
As referenced in Stack Overflow question Inplace substitution from ConfigParser, you’re looking for eval()
:
print eval('self.post.id') # Prints the value of self.post.id