Reference Variables In Python Like Those In PHP

Neil Cerutti horpner at yahoo.com
Tue Aug 15 10:47:54 EDT 2006


On 2006-08-15, Chaos <psnim2000 at gmail.com> wrote:
> 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?

If you store an item in a one-element list, you can use the list
like a reference, but it's not syntactically transparent.

>>> x = [1]
>>> y = x

Now x and y refer to the same list. Now you can change the
elements in the list through y.

>>> y[0] += 1
>>> x
[2]
>>> y
[2]

-- 
Neil Cerutti
Sermon Outline: I. Delineate your fear II. Disown your fear III.
Displace your rear --Church Bulletin Blooper



More information about the Python-list mailing list