Can anyone tell why, why the length of my instance variable( with type set) do not work the way as I though.
When I called a.addToSet("xx")
, I thought the value of len(b.aSet)
and len(ExampleClass.aSet)
should return zero, so as a.aInt
be 100 and rest be 0, while the console print the 1 for all the three instance.
Many thanks
==========================
Let's say, we have a class called ExampleClass with two variable: aSet and aInt.
class ExampleClass():
aSet = set()
aInt = 0
def addToSet(self,aValue):
self.aSet.add(aValue)
a=ExampleClass()
b=ExampleClass()
a.addToSet("xx")
a.aInt=100
print len(a.aSet)
print len(b.aSet)
print len(ExampleClass.aSet)
print a.aInt
print b.aInt
print ExampleClass.aInt
OUTPUT
1
1
1
100
0
0