Reference Variables In Python Like Those In PHP

C

Chaos

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?
 
T

Terry Reedy

Chaos said:
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
 
N

Neil Cerutti

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.

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

Luke Plant

Chaos said:
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
 
F

Fredrik Lundh

Luke said:
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>
 
L

Laurent Pointal

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.2

A+

Laurent.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top