I’m trying to instantiate a class object whose desired name is held in a variable that’s created elsewhere.
I can’t figure out how to make the instance of the class have the name held by the variable.
example:
class foo:
def __init__(self):
self.var1 = "bar"
if __name__ == "__main__":
test = "a" # returned by another func.
[string held by variable 'test'] = foo()
print a.var1
Is this possible? (or even advisable…)
It is not advisable, since it makes it difficult to program with the variable
awhen you do not know its name until run-time.You might think about using a dict instead: