View Full Version : [SOLVED] Python list of classes issue

February 20th, 2012, 06:28 PM
Hi guys,

I'm running into trouble with some Python stuff. I have a list that has seven copies of a class and I am intending to change just the values of one of them but whenever I do it changes the value for all.

Here is the class:

class Node:
previous = -1
distFromSrc = 1000000
visited = False

And here is how I create the list:

def createNodeTable(network):
nodeTable = []
for line in network:
return nodeTable

'network' is a list of length 7 so when I print the 'nodeTable[x].visited' before I attempt to make any changes to the values I get 'False' for every one.

If I call the following function however all of the '.visited' values change to false not just the one I am intending to change.

Whatever 'currentNode' is changed to I get the same issue:

def setVisited(currentNode, nodeTable):
nodeTable[currentNode].visited = True
return nodeTable

Is there an issue with the setVisited function or is it to with the nodeTable? I don't even know where to start to try to fix this.

February 20th, 2012, 07:11 PM
I'm no Python expert, but doesn't an instance variable need to be prefixed with self?

class Node:
def __init__(self):
self.previous = -1
self.distFromSrc = 1000000
self.visited = False

Otherwise they will be class variables.

February 20th, 2012, 08:15 PM
That's one way of fixing it. The way I found after however was just to change the append line to Node() so each is a new instance.

Tony Flury
February 21st, 2012, 10:00 AM
you have to do both I think.

Your original code creates a new instance once, and appends it to the list 7 times, and creates within the class a set of Class variables - i.e. every instance of the class you create will share the same data.