[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.python

Re: assigning values in python and perl

J. Peng

1/17/2008 3:41:00 AM

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.kyo@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
>
> $ 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
>
4 Answers

Steven D'Aprano

1/17/2008 4:20:00 AM

0

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-p...
by-value/

and

http://codertricks.strainu.ro/java/2007/06/15/call-b...


Did you read the links I sent you yesterday, especially the second one?

http://effbot.org/zone/python-o...
http://effbot.org/zone/call-by-...



--
Steven

Mel

1/17/2008 5:55:00 AM

0

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


J. Peng

1/17/2008 6:12:00 AM

0

On Jan 17, 2008 1:54 PM, Mel <mwilson@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.

Antoon Pardon

1/22/2008 12:24:00 PM

0

On 2008-01-17, Steven D'Aprano <steven@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