Pointers

Justin Sheehy dworkin at ccs.neu.edu
Fri Mar 17 15:26:06 EST 2000


Curtis Jensen <cjensen at bioeng.ucsd.edu> writes:

> If the data in the dictionary changes, I want the data in the class to
> automaticaly change also.  Is this possible?

Is there a reason why you can't just contain a reference to the
dictionary in the class?  An example that is probably not sufficient
for your needs but that illustrates my point:

class with_d:
    def __init__(self, d):
        self.d = d

You could then do things like:

>>> d = {}
>>> d['A'] = 1
>>> d['B'] = 0
>>> a = with_d(d)
>>> d['B'] = 2
>>> a.d['A']
1
>>> a.d['B']
2
>>> b = with_d(d)
>>> b.d['A'] = 3
>>> a.d['A']
3
>>> 

You could employ various bits of trickery to make this prettier; I am
only trying to demonstrate the general idea.

-Justin

 




More information about the Python-list mailing list