sharing dictionaries amongst class instances

Bengt Richter bokr at oz.net
Sun Nov 9 23:56:46 CET 2003


On Sun, 09 Nov 2003 20:22:36 GMT, "Kerry Neilson" <kmneilso at REMOVEyahoo.com> wrote:

>Hi,
>Really hung up on this one.  I'm trying to get all the fields of a
>dictionary to be unique for each class:
>
Please post something that at least compiles

 >>> class A {
   File "<stdin>", line 1
     class A {
            ^
 SyntaxError: invalid syntax


>class A {
         ^--this must be left over from some abortive experiment
>  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()
This looks like you are not showing an import A, assuming that class A is defined in A.py

>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
I suggest you copy and paste from the session where you did that. Otherwise
you're wasting your time and everyone else's.

>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.
The above doesn't work period, so there is no telling what you actually did,
and I might be able to guess what you are trying to do, but I don't really want to, sorry.

>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.
>
See above, guess I need a snack ;-)

Regards,
Bengt Richter




More information about the Python-list mailing list