Pointers
Kay Schluehr
K.Schluehr at ALCATEL.de
Thu Mar 16 06:04:54 EST 2000
Hello ,
You could tackle this problem also in a
more object-oriented style:
class C:
d = {'A':0, 'B':0} # class variable
def __init__(self):
self.a=A()
self.b=B()
def ChangeDict(self, key,value):
self.d[key]=value
if key = ='A':
self.a.ChangeVal()
elif key = = 'B':
self.b.ChangeVal()
else: [...]
class A(C):
def __init__(self):
[...]
def ChangeVal(self):
self.a=self.d['A']
class B(C):
def __init__(self):
[...]
def ChangeVal(self):
self.b=self.d['A']
Now You controll change by using an instance of C:
c=C()
c.ChangeDict('A',1)
c.ChangeDict('B',2)
should do what You want.
'there is always more than one way to do it' :-)
Regards K.S.
Curtis Jensen 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?
>
> --
> Curtis Jensen
> cjensen at be-research.ucsd.edu
> http://www-bioeng.ucsd.edu/~cjensen/
> FAX (425) 740-1451
More information about the Python-list
mailing list