Pointers

Curtis Jensen cjensen at bioeng.ucsd.edu
Thu Mar 16 00:31:18 CET 2000


Richard Jones wrote:
> 
> [Curtis Jensen]
> > Pointers are sometimes usefull.  I have a module that creates a
> > dictionary.  I want to include some elements from the distionary in one
> > class and other elements in another class.  If I had pointers, I could
> > do that.  Is there another way?
> 
>    As I explained in my response to your post (unlike the unhelpful Bjorn ;)
> you're already dealing with a reference there. The dictionary is an object and
> your only control over it is via your reference to it. You can make labels
> (variables) for that reference, or pass that reference around to functions or
> methods. I think you'll find that you can already do what you want:
> 
> >>> class A:
> ...  def foo(self, a):
> ...   a['A'] = 1
> ...
> >>> class B:
> ...  def foo(self, b):
> ...   b['B'] = 1
> ...
> >>> a=A()
> >>> b=B()
> >>> d={}
> >>> a.foo(d)
> >>> b.foo(d)
> >>> d
> {'B': 1, 'A': 1}
> 
>         Richard

Maybe I'm missunderstanding, but it appears that this is just a copy
routine.  I want something like this:

>>> 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