[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

assigning values in python and perl

J. Peng

1/17/2008 3:35:00 AM

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 Answers

George Sakkis

1/17/2008 5:27:00 AM

0

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

Christian Heimes

1/17/2008 6:03:00 AM

0

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

Christian

J. Peng

1/17/2008 6:07:00 AM

0

On Jan 17, 2008 2:03 PM, Christian Heimes <lists@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-...
>

Yes I agree. Not the same at all.

Paddy

1/17/2008 6:11:00 AM

0

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.

George Sakkis

1/17/2008 6:26:00 AM

0

On Jan 17, 1:03 am, 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 readhttp://effbot.org/zone/call-by-...
>
> 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

Hrvoje Niksic

1/17/2008 6:55:00 AM

0

"J. Peng" <peng.kyo@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.

Christian Heimes

1/17/2008 6:59:00 AM

0

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

1/17/2008 7:21:00 AM

0

On Jan 17, 2008 2:55 PM, Hrvoje Niksic <hniksic@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

1/17/2008 8:33:00 AM

0

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