Re: assigning values in python and perl

Discussion in 'Python' started by J. Peng, Jan 17, 2008.

  1. J. Peng

    J. Peng Guest

    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 <> 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
    >
    J. Peng, Jan 17, 2008
    #1
    1. Advertising

  2. 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
    Steven D'Aprano, Jan 17, 2008
    #2
    1. Advertising

  3. J. Peng

    Mel Guest

    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 <> 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
    Mel, Jan 17, 2008
    #3
  4. J. Peng

    J. Peng Guest

    On Jan 17, 2008 1:54 PM, Mel <> 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.
    J. Peng, Jan 17, 2008
    #4
  5. On 2008-01-17, Steven D'Aprano <> 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
    Antoon Pardon, Jan 22, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Johnsy Joseph
    Replies:
    4
    Views:
    4,357
    Jim Lewis
    Sep 21, 2004
  2. Guest

    assigning values to form inputs

    Guest, Aug 24, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    517
  3. Manuel Graune

    Objects, lists and assigning values

    Manuel Graune, Apr 5, 2007, in forum: Python
    Replies:
    5
    Views:
    239
    7stud
    Apr 7, 2007
  4. J. Peng
    Replies:
    9
    Views:
    280
    George Sakkis
    Jan 17, 2008
  5. weston
    Replies:
    1
    Views:
    234
    Richard Cornford
    Sep 22, 2006
Loading...

Share This Page