sharing dictionaries amongst class instances
Kerry Neilson
kmneilso at REMOVEyahoo.com
Sun Nov 9 15:22:36 EST 2003
Hi,
Really hung up on this one. I'm trying to get all the fields of a
dictionary to be unique for each class:
class A {
my_dict = []
dict_entry = { 'key1':0, 'key2':0 }
__init__(self):
for x in range(10):
tmp = copy.deepcopy(self.dict_entry)
tmp['key1'] = x
self.my_dict.append(tmp)
}
in a driver, I have
inst0, inst1 = A.A(), A.A()
inst0.my_dict[1]['key2'] = "ted"
inst1.my_dict[5]['key2'] = "steve"
inst0.display()
inst1.display()
printing them out shows that both objects have ted and steve in their
dictionaries. I have done this very thing outside of a class, and it works
wonderfully. All other attributes in the class are able to remain unique.
ie, inst0.data = 5 and inst1.data = 8 works fine.
I believe I could set the dictionaries up beforehand, but they will be
substatially different sizes, and it certainly wouldn't be proper.
Any ideas? Thanks very much for any insight.
More information about the Python-list
mailing list