6
Take a look at this class:
class Signal:
point = None
view = {
"x": None,
"y": None,
}
def __init__(self, point):
self.point = point
self.set_position()
print(self.point + " -> " + str(self.view["x"]) + "x" + str(self.view["y"]))
def set_position(self):
if self.point == "north":
self.view["x"] = 150
self.view["y"] = 200
else:
self.view["x"] = 300
self.view["y"] = 400
def check(self):
print(self.point + " -> " + str(self.view["x"]) + "x" + str(self.view["y"]))
And now in the following result to instantiate 2 different variables like this object and check the values:
>>> s1 = Signal("north")
north -> 150x200
>>> s2 = Signal("south")
south -> 300x400
>>> s1.check()
north -> 300x400
I just can’t find the logic of it. Can you explain why when creating the s2
the values of s1
are exchanged?
view
is dictionary type and is defined as class variable, not instance: Class and Instance Variables (documentation in English)– Gomiero