assigning values in python and perl

J

J. Peng

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
 
G

George Sakkis

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);
}
}

$ gcc -std=c99 test.c
$ ./a.out
-1 2 3


Hope this helps,
George
 
P

Paddy

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

George Sakkis

George said:
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
 
H

Hrvoje Niksic

J. Peng said:
$ 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.
 
C

Christian Heimes

George said:
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

J. Peng

@$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
 
G

George Sakkis

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
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top