I am trying to make a function that creates classes, and append these classes to a list.
When I run the program: I get the following error:
NameError: name ‘jackie’ is not defined
This implies can only use the instance inside the function. How can I make it global?, I tried global eval('jackie') but doesn’t work.
population =[]
class person():pass
def createdarwin(name):
global population
darwin=p.image.load('darwin.png')
vars()[name]=person(darwin)
population.append(name)
def main():
createdarwin('jackie')
for i in population:
eval(i).update()
If you want variably-named variables, then use a dictionary instead. Then you don’t need
vars()and you don’t needeval():