I created a node class like this:
def __init__(self, data, incoming = [], outgoing = []):
self.data = data
self.incoming = incoming
self.outgoing = outgoing
def addIncoming(self, incoming):
self.incoming.append(incoming)
def addOutgoing(self, outgoing):
self.outgoing.append(outgoing)
where every instance was initialized like Node(number)
so the incoming and outgoing parameters were defaulted to an empty list. I also had functions that appended other nodes to the incoming and outgoing list but when I did that, it was appending to every instance. For example lets say I had a=Node(1)
and b=Node(2)
. If I did a.addIncoming(b)
and printed a.incoming
and b.incoming
, they both would print Node b
. Why are these values shared between separate instances?
When I updated my init function to be
def __init__(self, data, incoming = [], outgoing = []):
self.data = data
self.incoming = []
self.outgoing = []
everything worked as expected but I feel like the two should work the same. What am I misunderstanding here?