I am trying to create a method that allows each instance of the class to be "neighbors" of each other. If Entity A adds B as a neighbor, then B is in A's neighbor_list. However, as the output below shows, B is automatically added to B's neighbor list and that's not the desired behavior - B's neighbor list should be empty. Any thoughts?
Output:
a's neighbor list element: b
b's neighbor list element: b
Code:
class Entities:
neighbor_list = []
name = ''
def __init__(self,name):
self.name = name
def add (self, neighbor):
self.neighbor_list.append(neighbor)
a = Entities ('a')
b = Entities ('b')
a.add(b)
print "a's neighbor list element: %s" % a.neighbor_list[0].name
print "b's neighbor list element: %s" % b.neighbor_list[0].name
neighbor_listis a class member rather than an instance member, because he assumed that Python did members exactly like, say, Java.