Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

assigning values in python and perl

1 view
Skip to first unread message

J. Peng

unread,
Jan 16, 2008, 10:34:33 PM1/16/08
to pytho...@python.org
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

J. Peng

unread,
Jan 16, 2008, 10:40:59 PM1/16/08
to pytho...@python.org
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?

Steven D'Aprano

unread,
Jan 16, 2008, 11:20:14 PM1/16/08
to
On Thu, 17 Jan 2008 11:40:59 +0800, J. Peng wrote:

> 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

George Sakkis

unread,
Jan 17, 2008, 12:27:16 AM1/17/08
to
On Jan 16, 10:34 pm, "J. Peng" <peng....@gmail.com> wrote:

> 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

Mel

unread,
Jan 17, 2008, 12:54:44 AM1/17/08
to
J. Peng wrote:
> 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?
>
> On Jan 17, 2008 11:34 AM, J. Peng <peng...@gmail.com> wrote:
>> 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

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


Christian Heimes

unread,
Jan 17, 2008, 1:03:03 AM1/17/08
to pytho...@python.org
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

J. Peng

unread,
Jan 17, 2008, 1:07:02 AM1/17/08
to pytho...@python.org
On Jan 17, 2008 2:03 PM, Christian Heimes <li...@cheimes.de> wrote:

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

Paddy

unread,
Jan 17, 2008, 1:10:55 AM1/17/08
to
On Jan 17, 3:34 am, "J. Peng" <peng....@gmail.com> wrote:
> 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.

J. Peng

unread,
Jan 17, 2008, 1:11:57 AM1/17/08
to Mel, pytho...@python.org
On Jan 17, 2008 1:54 PM, Mel <mwi...@the-wire.com> wrote:
>
> 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.
>

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.

George Sakkis

unread,
Jan 17, 2008, 1:25:38 AM1/17/08
to

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

Christian Heimes

unread,
Jan 17, 2008, 1:59:05 AM1/17/08
to pytho...@python.org
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

Hrvoje Niksic

unread,
Jan 17, 2008, 1:55:29 AM1/17/08
to
"J. Peng" <peng...@gmail.com> writes:

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

J. Peng

unread,
Jan 17, 2008, 2:20:36 AM1/17/08
to pytho...@python.org
On Jan 17, 2008 2:55 PM, Hrvoje Niksic <hni...@xemacs.org> wrote:
>
> @$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

George Sakkis

unread,
Jan 17, 2008, 3:33:25 AM1/17/08
to
On Jan 17, 1:59 am, Christian Heimes <li...@cheimes.de> wrote:
> 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

Antoon Pardon

unread,
Jan 22, 2008, 7:23:32 AM1/22/08
to
On 2008-01-17, Steven D'Aprano <ste...@REMOVE.THIS.cybersource.com.au> wrote:
> On Thu, 17 Jan 2008 11:40:59 +0800, J. Peng wrote:
>
>> 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.

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

0 new messages