assigning values in python and perl

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

  1. J. Peng

    J. Peng Guest

    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 Jan 16, 10:34 pm, "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.
    >
    > (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
     
    George Sakkis, Jan 17, 2008
    #2
    1. Advertising

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

    J. Peng Guest

    On Jan 17, 2008 2:03 PM, Christian Heimes <> 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.
     
    J. Peng, Jan 17, 2008
    #4
  5. J. Peng

    Paddy Guest

    On Jan 17, 3: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]
    >


    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.
     
    Paddy, Jan 17, 2008
    #5
  6. On Jan 17, 1:03 am, Christian Heimes <> 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-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
     
    George Sakkis, Jan 17, 2008
    #6
  7. "J. Peng" <> 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.
     
    Hrvoje Niksic, Jan 17, 2008
    #7
  8. 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
     
    Christian Heimes, Jan 17, 2008
    #8
  9. J. Peng

    J. Peng Guest

    On Jan 17, 2008 2:55 PM, Hrvoje Niksic <> 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
     
    J. Peng, Jan 17, 2008
    #9
  10. On Jan 17, 1:59 am, Christian Heimes <> 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
     
    George Sakkis, Jan 17, 2008
    #10
    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,386
    Jim Lewis
    Sep 21, 2004
  2. Guest

    assigning values to form inputs

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

    Objects, lists and assigning values

    Manuel Graune, Apr 5, 2007, in forum: Python
    Replies:
    5
    Views:
    252
    7stud
    Apr 7, 2007
  4. J. Peng
    Replies:
    4
    Views:
    332
    Antoon Pardon
    Jan 22, 2008
  5. weston
    Replies:
    1
    Views:
    253
    Richard Cornford
    Sep 22, 2006
Loading...

Share This Page