These two class definitions are structured identically, except in the weird() class definition the attribute 'd' is a list, and in the normal() class definition its an int. I dont understand why the weird() class results in self.d = d, while this is not the case with the class normal(). Why does Python treat int and list differently in this situation?
class weird:
def __init__(self):
d = [1,2,3]
self.d = d
for x in range(3):
d[x] = 10+x
print "d =", d
print "self.d =", self.d
class normal:
def __init__(self):
d = 1
self.d = d
d = 11
print "d =", d
print "self.d =", self.d
And when I run the code, I get
>>> a=weird()
d = [10, 11, 12]
self.d = [10, 11, 12]
>>> b=normal()
d = 11
self.d = 1