Does anybody know why this below code prints 0 and 1 rather than 5 and 2, in csharp similar code would print 5 and 2 and I am just trying to work out the logic here.
class Myclass:
a = 0
b = 1
def foo():
for x in range(1):
for y in range(1):
myclass = Myclass()
if y == 1:
myclass.a = 5
if y == 1:
myclass.b = 2
ClassList.append(Myclass)
for x in ClassList:
print x.a
print x.b
ClassList = []
foo()
The reason is that
range(1)returns[0], not[0, 1], so youry == 1test never evaluates to true.Also, you’re appending
Myclassrather thanmyclass— that is, the actual class, rather than the instance you created — to the list, so you’re always printing the unmodifiedaandbfrom the class.