Reference Variables In Python Like Those In PHP

Laurent Pointal laurent.pointal at limsi.fr
Wed Aug 16 04:47:47 EDT 2006


Chaos a écrit :
> Is It possible to have reference variables like in PHP
> 
> ex.
> 
> <?php
> 
> $x = 1;
> $y =& $x;
> 
> $y += 1;
> 
> echo $x;
> echo "\n"
> echo $y;
> 
> ?>
> 
> This would show
> 
> 2
> 2
> 
> Is this available in python?

See other replies (ie. in Python all variables are names refering to
objects).

You may achieve same result using an object as a namespace, like this:

Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class Dummy(object) : pass
...
>>> x=Dummy()
>>> x.val = 1
>>> y = x	<-- here x and y refer to *same* Dummy object
>>> y.val += 1
>>> x.val
2
>>> y.val
2

A+

Laurent.



More information about the Python-list mailing list