469,364 Members | 2,398 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,364 developers. It's quick & easy.

Reference Variables In Python Like Those In PHP

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?

Aug 15 '06 #1
5 1484

"Chaos" <ps*******@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
Is It possible to have reference variables like in PHP
<?php
$x = 1;
$y =& $x;
$y += 1;
echo $x;
echo "\n"
echo $y;
?>

This would show
2
2
Is this available in python?
No, in the literal meaning of your question. But..
1. The concept 'reference variable' pertains to PHP's object model, which
is different from Python's.
2. They are means, not ends. So a possibly more useful question would be
along the lines of "In PHP, I do this with a reference variable. How can I
accomplish the same goal in Python?"

Terry Jan Reedy

Aug 15 '06 #2
On 2006-08-15, Chaos <ps*******@gmail.comwrote:
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
Aug 15 '06 #3

Chaos wrote:
Is It possible to have reference variables like in PHP
....
Is this available in python?
You should note that, to a nearest equivalent, all variables are
reference variables in Python. The difference is in what assignment
does - += in Python does an assignment of a new object for immutable
objects. For mutable objects like lists, += does an in place
modification.

x = 1
y = x # y and x now point to the same object
y += 1 # not any more, because ints are immutable,
# and += is defined not to mutate for ints

Luke

Aug 15 '06 #4
Luke Plant wrote:
You should note that, to a nearest equivalent, all variables are
reference variables in Python. The difference is in what assignment
does - += in Python does an assignment of a new object for immutable
objects. For mutable objects like lists, += does an in place
modification.
that depends on the object implementation, though -- the "+=" statement
does not, in itself, distinguish between mutable and immutable objects.

</F>

Aug 15 '06 #5
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.
Aug 16 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.