I just thought python's way of assigning value to a variable is really
different to other language like C,perl. :)
Below two ways (python and perl) are called "pass by reference", but
they get different results.
Yes I'm reading 'Core python programming', I know what happened, but
just a little confused about it.
$ cat t1.py
def test(x):
x = [4,5,6]
a=[1,2,3]
test(a)
print a
$ python t1.py
[1, 2, 3]
$ cat t1.pl
sub test {
my $ref = shift;
@$ref = (4,5,6);
}
my @a = (1,2,3);
test(\@a);
print "@a";
$ perl t1.pl
4 5 6 9 2131
On Jan 16, 10:34 pm, "J. Peng" <peng....@gmail.comwrote:
I just thought python's way of assigning value to a variable is really
different to other language like C,perl. :)
Below two ways (python and perl) are called "pass by reference", but
they get different results.
(snipped)
Python's parameter passing is like passing a pointer in C/C++. You can
modify the object that is pointed by the pointer (if the object is
mutable), but reassigning the pointer (i.e. making it point to
something else) has no effect outside the function. Consider the
following programs in Python and C:
#==== Python version ====================
def test(x):
y = [4,5,6]
# modifies the passed object
x[0] = -x[0];
# rebinds the name x to the object referenced by y; no effect on
passed object
x = y
x = [1,2,3]
test(x)
print x
$ python test.py
[-1, 2, 3]
#==== C version ====================
#include <stdio.h>
void test(int x[]) {
int y[] = {4,5,6};
// modifies the passed array
x[0] = -x[0];
// reassigns the pointer to the local array pointed by y; no effect
on passed array
x = y;
}
int main(int argc, char* argv[]) {
int x[] = {1,2,3};
test(x);
for (int i=0; i<3; i++) {
printf("%d ", x[i]);
}
}
$ gcc -std=c99 test.c
$ ./a.out
-1 2 3
Hope this helps,
George
George Sakkis wrote:
Python's parameter passing is like passing a pointer in C/C++.
[snip]
It's not (I repeat NOT) like passing a pointer in C. Please read http://effbot.org/zone/call-by-object.htm
Christian
On Jan 17, 2008 2:03 PM, Christian Heimes <li***@cheimes.dewrote:
George Sakkis wrote:
Python's parameter passing is like passing a pointer in C/C++.
[snip]
It's not (I repeat NOT) like passing a pointer in C. Please read http://effbot.org/zone/call-by-object.htm
Yes I agree. Not the same at all.
On Jan 17, 3:34 am, "J. Peng" <peng....@gmail.comwrote:
I just thought python's way of assigning value to a variable is really
different to other language like C,perl. :)
Below two ways (python and perl) are called "pass by reference", but
they get different results.
Yes I'm reading 'Core python programming', I know what happened, but
just a little confused about it.
$ cat t1.py
def test(x):
x = [4,5,6]
Hi J,
Unlike C or Perl , there is hardly ever any good reason for functions
to act by purposefully modifying their arguments. Don't do it. Stick a
return in your function and use that result. It is much more
maintainable and readable. (It will also work, and look a lot more
like, the same function written in other languages :-)
- Paddy.
On Jan 17, 1:03 am, Christian Heimes <li...@cheimes.dewrote:
George Sakkis wrote:
Python's parameter passing is like passing a pointer in C/C++.
[snip]
It's not (I repeat NOT) like passing a pointer in C. Please readhttp://effbot.org/zone/call-by-object.htm
Christian
Posting a counter-example where the difference is clearly shown would
be more vastly useful than referring to a list of long obscure usenet
posts with practically no examples. C/C++ are not even mentioned in
that page. I am not claiming you are wrong, I just don't find
particularly this page particularly enlightening.
George
George Sakkis wrote:
Posting a counter-example where the difference is clearly shown would
be more vastly useful than referring to a list of long obscure usenet
posts with practically no examples. C/C++ are not even mentioned in
that page. I am not claiming you are wrong, I just don't find
particularly this page particularly enlightening.
I don't find a posting like "It's call-by-reference, but in fact it's
doesn't behave like call-by-reference" helpful. The text explains
Python's calling convention on a CS level. Please trust me that the
explanation on the site is right. It was written by somebody who
designed and wrote parts of Python.
Christian
"J. Peng" <pe******@gmail.comwrites:
$ cat t1.py
def test(x):
x = [4,5,6]
a=[1,2,3]
test(a)
print a
$ python t1.py
[1, 2, 3]
$ cat t1.pl
sub test {
my $ref = shift;
@$ref = (4,5,6);
}
@$ref = (4, 5, 6) intentionally assigns to the same list pointed to by
the reference. That would be spelled as x[:] = [4, 5, 6] in Python.
What Python does in your example is assign the same as Perl's $ref =
[4, 5, 6]. So they're not so different after all.
On Jan 17, 2008 2:55 PM, Hrvoje Niksic <hn*****@xemacs.orgwrote:
>
@$ref = (4, 5, 6) intentionally assigns to the same list pointed to by
the reference. That would be spelled as x[:] = [4, 5, 6] in Python.
What Python does in your example is assign the same as Perl's $ref =
[4, 5, 6]. So they're not so different after all.
Yup,you're so right.This test below in perl is the same as in python.
So at before I may got mistaken by myself.Thanks all.
$ cat t1.pl
sub test {
my $ref = shift;
$ref = [4,5,6];
}
my @a = (1,2,3);
test(\@a);
print "@a";
$ perl t1.pl
1 2 3
On Jan 17, 1:59 am, Christian Heimes <li...@cheimes.dewrote:
George Sakkis wrote:
Posting a counter-example where the difference is clearly shown would
be more vastly useful than referring to a list of long obscure usenet
posts with practically no examples. C/C++ are not even mentioned in
that page. I am not claiming you are wrong, I just don't find
particularly this page particularly enlightening.
I don't find a posting like "It's call-by-reference, but in fact it's
doesn't behave like call-by-reference" helpful.
You must be referring to other poster then; I did not mention at all
the term "call by reference" (or "term by value" for that matter)
exactly because they mean different things to different people. I only
stated that Python's parameter passing is like passing a pointer in C/C
++ and gave a specific example of what I mean by "like passing a
pointer", without labeling it as "call-by-X".
The text explains
Python's calling convention on a CS level. Please trust me that the
explanation on the site is right. It was written by somebody who
designed and wrote parts of Python.
I didn't dispute the validity of the explanation, only its educational
usefulness (both with respect to my posting and in general for someone
new to Python).
George This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Matthew Sims |
last post by:
Python Newbie here. This is my first time learning object-oriented
programming and trying to break out of the usual Korn/Perl/PHP style
of programming. Having some difficulty understand some items....
|
by: Fred Ma |
last post by:
Hello,
This is not a troll posting, and I've refrained from
asking because I've seen similar threads get all
nitter-nattery. But I really want to make a decision
on how best to invest my time....
|
by: drife |
last post by:
Hello,
Making the transition from Perl to Python, and have a
question about constructing a loop that uses an iterator
of type float. How does one do this in Python?
In Perl this construct...
|
by: surfunbear |
last post by:
I've read some posts on Perl versus Python and studied a bit of my
Python book.
I'm a software engineer, familiar with C++ objected oriented
development, but have been using Perl because it is...
|
by: Lad |
last post by:
Is anyone capable of providing Python advantages over PHP if there are
any?
Cheers,
L.
|
by: squash |
last post by:
I am a little annoyed at why such a simple program in Perl is causing
so much difficulty for python, i.e:
$a += 200000 * 140000;
print $a;
|
by: Preben Randhol |
last post by:
Hi
A short newbie question. I would like to extract some values from a
given text file directly into python variables. Can this be done simply
by either standard library or other libraries? Some...
|
by: Roy Smith |
last post by:
I'm working on a product which for a long time has had a Perl binding for
our remote access API. A while ago, I wrote a Python binding on my own,
chatted it up a bit internally, and recently had a...
|
by: miken32 |
last post by:
In PHP, if a function returns an array it's fairly common to capture
its return values like this:
<?php
list($foo, $bar, $baz) = some_function_that_return_an_array();
?>
In Javascript, would...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |