Pointers
Will Ware
wware at world.std.com
Wed Mar 15 19:04:57 EST 2000
Curtis Jensen (cjensen at bioeng.ucsd.edu) wrote:
> >>> d={'A':0,'B':0}
> >>> class A:
> ... def __init__(self):
> ... self.a -> (Pointer to) d['A']
> ...
> >>> class B:
> ... def __init__(self):
> ... self.b -> (Pointer to) d['B']
> >>> a=A()
> >>> b=B()
> >>> print a.a
> 0
> >>> print b.b
> 0
> >>> d['A'] = 1
> >>> d['B'] = 2
> >>> print a.a
> 1
> >>> print b.b
> 2
> If the data in the dictionary changes, I want the data in the class to
> automaticaly change also. Is this possible? Or at least something
> close?
OK, I see the rub here. When you type "d['A'] = 1", the thing to which
a.a referred still exists, but now d['A'] is referring to something else.
You might need to do an on-the-fly dictionary lookup, like this:
>>> d={'A':0,'B':0}
>>> class A:
... def __init__(self):
... self.dict = d
... def a(self):
... return self.dict['A']
That should do it for you, I think, albeit inefficient. The inefficiency
results because you care what happens when 'd' has one of its entries
reassigned.
--
- - - - - - - - - - - - - - - - - - - - - - - -
Resistance is futile. Capacitance is efficacious.
Will Ware email: wware @ world.std.com
More information about the Python-list
mailing list