Saturday, 24 August 2013

Class variables behave differently for list and int?

Class variables behave differently for list and int?

The class shared variables are shared with all the instances of the
classes as far as I know. But I am having trouble getting my head around
this.
class c():
a=[1]
b=1
def __init__(self):
pass
x=c()
x.a.append(1)
x.b+=1 #or x.b=2
print x.a #[1,1]
print x.b #2
y=c()
print y.a #[1,1] :As Expected
print y.b #1 :why not 2?
y.a resonates with the x.a but y.b doesn't.
hope someone can clarify.
EDIT: And how can the same functionality be created for ints.

No comments:

Post a Comment