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
> May I ask, python's pass-by-reference is passing the object's reference
> to functions, but perl, or C's pass-by-reference is passing the variable
> itself's reference to functions. So althought they're all called
> pass-by-reference,but will get different results.Is it?
Python is not call by reference.
Any book or person that says it is, is wrong to do so.
Python's function call semantics are not the same as C, or Perl, or
Pascal. They are, however, similar to those of Lisp, Scheme, Emerald and
especially CLU. It is neither pass by reference, nor pass by value.
Java also uses similar call-by-object (sometimes call-by-sharing)
semantics, and just like Python, Java developers tie themselves in knots
by using misleading names for it:
http://codertricks.strainu.ro/java/2007/05/02/why-java-sends-parameters-
by-value/
and
http://codertricks.strainu.ro/java/2007/06/15/call-by-sharing/
Did you read the links I sent you yesterday, especially the second one?
http://effbot.org/zone/python-objects.htm
http://effbot.org/zone/call-by-object.htm
--
Steven
> 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
As I said somewhere else, it's all about binding to names in namespaces.
a=[1,2,3]
creates a list and binds it with the name 'a' in the current namespace.
test(a) (along with def test(x)) takes the object named 'a' in the
current namespace and binds it with the name 'x' in function test's
local namespace. So, inside test, the name 'x' starts by referring to
the list that contains [1,2,3]. But the only use test makes of the
name 'x' is to re-bind it to a new list [4,5,6]. Exiting test, the
local namespace is thrown away.
You could change your program a bit to clarify what's going on:
def test(x):
print "x=", x
x = [4,5,6]
print "x=", x
a=[1,2,3]
test(a)
print "a=", a
Cheers, Mel
It's not (I repeat NOT) like passing a pointer in C. Please read
http://effbot.org/zone/call-by-object.htm
Christian
Yes I agree. Not the same at all.
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.
Hi,
Yes I know it pretty well now, but thanks for the pointer.
What I asked is that, if we re-write that code with perl or C, we'll
get different results.
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
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
> $ 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.
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
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
I don't think it is the function call semantics that are so different
as it is the assignment itself that is different.
an assignment in C, doesn't bind a new object to the name, but
stores new information in the object.
Trying to explain the different behaviour of C and python of
examples calling function that assign to a parameter, without
explaining how the assignment works will IMO not give people
enough to understand what is happening.
--
Antoon Pardon