Hello fine people of Stackoverflow!
I have a problem. I am trying to change an instance variable (which is on it's own a class instance) and for some reason, it changes the same instance variable for all instances of the original class.
here is the code:
#create State class
class State(object):
awake = False
#Set Person Class
class Person(object):
state = State()
#create person instences
jack = Person()
jill = Person()
print(jack.state.awake)
print(jill.state.awake)
#wake jill only
jill.state.awake = True
print("")
print(jack.state.awake)
print(jill.state.awake)
OUTPUT:
False
False
True
True
I am trying to wake jill only.
For
awake
andstate
to be instance variables they'd have to be bound to aself
instance, instead of at the class level. Otherwise class variables are static, in other words share state across all instances of that class.Then it works as you described