Raj Kumar
Raj Kumar

Reputation: 1587

Python creating static objects which are shared amongst all class objects

I have a class node something like this. It's a typical node object for a graph.

class Node(object):
    def __init__(self, data, edges = []):
        super(Node, self).__init__()
        self.data = data
        self.edges = edges
        self.visited = False

    def addEdge(self, *args):
        print(self)
        self.edges.extend(args)
        print(self.edges)

I create two objects like this -

one = Node(1)
two = Node(2)

Next I add a pointer of two to one using the addEdge method defined above -

one.addEdge(two)

Now comes the surprising bit. When I check the values of one.edges and two.edges I get this -

one.edges [<main.Node object at 0x109ed3e50>]

two.edges [<main.Node object at 0x109ed3e50>].

If you see both the objects have gotten the value. I'm quite puzzled at this and have no idea why this is happening. Is this how python behaves? If so can you explain this behaviour?

Upvotes: 0

Views: 43

Answers (1)

Mark
Mark

Reputation: 92440

You need to be careful when using an array literal as a default value because you don't get a new array with each instance — you get a reference to the same one. In your example you will see:

>> one.edges is two.edges
True

You need to do something to make sure you get a new array each time. One things you can do is:

self.edges = list(edges)

Another option is:

def __init__(self, data, edges = None):

   if edges is None:
        self.edges = []
   else:
        self.edges = edges

But edges is still mutable so it may also lead to subtle bugs if the caller is not expecting it to be changed.

Upvotes: 3

Related Questions