I want to create instances of a class from a string user has typed in so I used exec( ) function. The problem is I can't access instance by its name outside the function. My first thought was that it is a problem with scope of a function, and I still think it is but when I put instances in a list I can access them, just not using their name. I'm not really sure what is happening here.. Is there a way so that I could access instances by their name, like thing1.properties
but outside the function because this is not my whole code so it would be messy to put all outside the function? Something like to create list of instances in a function and to "extract" all instances outside the function so I could access them outside the function. Here is the code:
class Things:
def __init__(self, properties):
self.properties = properties
listt = []
def create_instance():
exec("thing1=Things('good')")
listt.append(thing1)
create_instance()
print listt[0].properties
print thing1.properties