472,353 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 1571

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
by: Riccardo Rossi | last post by:
Hi all! How does Python pass arguments to a function? By value or by reference? Thanks, Riccardo Rossi.
15
by: Torsten Mohr | last post by:
Hi, i'd like to pass a reference or a pointer to an object to a function. The function should then change the object and the changes should be...
5
by: Javier Campos | last post by:
WARNING: This is an HTML post, for the sake of readability, if your client can see HTML posts, do it, it doesn't contain any script or virus :-) I...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.