I think my problem lies within my comprehension of how Python handle Objects. Here is the code that produce my "bug":
a1 = A("my name is A1")
a2 = A("my name is A2")
a1.add_b("my name is B1")
a1.add_b("my name is B2")
print "for {0} the content of b_dict is: {1}".format(a1.name, a1.b_dict)
print "for {0} the content of b_dict is: {1}".format(a2.name, a2.b_dict)
class A():
name = ''
b_dict = {}
def __init__(self, name_of_a):
self.name = name_of_a
def add_b(self, name_of_b):
self.b_dict[name_of_b] = B(name_of_b)
return
class B():
name = ''
def __init__(self, name_of_b):
self.name = name_of_b
Now, the print result is:
for my name is A1 the content of b_dict is: {'my name is B1': <__main__.B instance at 0x7f0db0e5bdd0>, 'my name is B2': <__main__.B instance at 0x7f0db0e5be18>}
for my name is A2 the content of b_dict is: {'my name is B1': <__main__.B instance at 0x7f0db0e5bdd0>, 'my name is B2': <__main__.B instance at 0x7f0db0e5be18>}
I don't understand why by adding B objects into the b_dict dictionary of a1, I somehow manage to add the same objects to the b_dict dictionary of a2. Please enlighten me as to why it is happening.
Thank you for your time.