A confusing problem with Python instance variable
Posted: Sat Dec 11, 2010 1:59 pm
E.g.
Create a python class say A
Class A:
def __init__(self, Variable=[]):
self.Variable=Variable
If I create a few instances without specifying Variable, say
X = A()
Y = A()
then X.Variable.append(something) will add that something to Y even though I defined Variable to be an instance variable. If I add
if Variable:
self.Variable=Variable
Else:
self.Variable = []
then the problem is solved.
I refuse to believe python is that dumb...Or am I just not following the right format?
Create a python class say A
Class A:
def __init__(self, Variable=[]):
self.Variable=Variable
If I create a few instances without specifying Variable, say
X = A()
Y = A()
then X.Variable.append(something) will add that something to Y even though I defined Variable to be an instance variable. If I add
if Variable:
self.Variable=Variable
Else:
self.Variable = []
then the problem is solved.
I refuse to believe python is that dumb...Or am I just not following the right format?