Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument
Unexpected connections between class instances
here is my code to build a class called Neuron:
class Neuron():
def __init__(self,connection=[]):
self.connection=connection
def makeConnection(self,other):
self.connection.append(other)
def getConnection(self):
return self.connection
as you see each object of this class has it's own connection to other objects of the same class. Now i build a group of Neurons :
P=[]
for i in range(5):
P.append(Neuron())
Now , i want to make connections between my neurons: let's do something simple:
P[1].makeConnection(P[2])
now when i check in python :
P[2].getConnection()
it gives me P[2] in it's connection list! i just applied makeConnection method on P[1], where on earth I connected P[2] to itself!?! please someone help!