list storing variables

Marko Rauhamaa marko at pacujo.net
Mon Feb 23 08:49:03 EST 2015


"ast" <nomail at invalid.com>:

> Is there a way to define a container object able to store some
> variables so that a change of a variable make a change in this object
> content ?
>
> I dont need this feature. It is just something I am thinking about.
>
> In C language, there is &A for address of A

In Python, you can accomplish "&" by creating an object:

   >>> a = 3
   >>> b = 4
   >>> class AmpersandA:
   ...     def get(self): return a
   ...     def set(self, value): global a; a = value
   ... 
   >>> class AmpersandB:
   ...     def get(self): return b
   ...     def set(self, value): global b; b = value
   ... 
   >>> l = [ AmpersandA(), AmpersandB() ]
   >>> for m in l:
   ...     m.set(7)
   ... 
   >>> print(a)
   7
   >>> print(b)
   7
   >>> 

It's exactly like "&" in C escept it's a slight bit more verbose and the
exact implementation is dependent on the name of the variable.


Marko



More information about the Python-list mailing list