How to do * and & C-lang. operations in Python?

Gerhard Häring g.haering at ___skynamics.com.invalid
Thu Sep 20 13:00:00 EDT 2001


Toni Gaya wrote:

> I have a variable defined on one module, and want to use _exactly_ the
> same variable in other module. Example:
> 
> module1:
> 	a = 1
> 
> module 2:
> 	import module1
> 	a = module1.a
> 	Now, I have module1.a value in a, not a reference to module1.a in a
> (module2.a).
> Assignaments are done by 'value', not by 'reference'. Function calls
> are done by 'reference'. How to do assignaments by reference?


Use instances of classes. Assignments work by reference with class 
instances.

 >>> class A:
...   def __init__(self, value):
...     self.value = value
...
 >>> x = A(5)
 >>> y = x
 >>> x
<__main__.A instance at 007E446C>
 >>> y
<__main__.A instance at 007E446C>
 >>> y.value
5
 >>> x.value = 6
 >>> y.value

You can see that x and y refer to the same class instance.

Gerhard
-- 
Gerhard Häring
skynamics AG
g.haering at skynamics.com
http://www.skynamics.com




More information about the Python-list mailing list