Re: no pass-values calling?

Discussion in 'Python' started by Neil Cerutti, Jan 16, 2008.

  1. Neil Cerutti

    Neil Cerutti Guest

    On Jan 15, 2008 10:09 PM, J. Peng <> wrote:
    > Hello,
    >
    > I saw this statement in Core Python Programming book,
    >
    > All arguments of function calls are made by reference, meaning that
    > any changes to these parameters within the function
    > affect the original objects in the calling function.


    Yes, that's generally correct. But you must be careful about what is
    meant by "changes to parameters". Assigning a new value to a parameter
    name (inside the function, a parameter is just a local variable) does
    not change the original object--it only rebinds the local variable to
    a new object.

    In the following function, a is rebound with an assignment statement,
    while b is mutated, i.e., changed, with an assignment statement.

    def f(a, b):
    a = 12
    b.value = 14

    Argument a will never be changed, while argument b will be. Python's
    argument passing semantics are extremely simple. It's the assignment
    statement that's tricky: some assignments mutate/change objects, and
    some only rebind names.

    > Does this mean there is not pass-values calling to a function in
    > python? only pass-reference calling? Thanks!


    Neither is quite true. Values are passed by binding parameter names to
    their corresponding arguments. This is similar to pass-by-reference in
    some cases (when the argument is mutated) but not in others (when the
    argument is not mutated). Thinking of it as pass-by-reference may help
    you to understand it, but bear in mind that Python's "references" may
    be rebound to new objects, which is quite different from the usual
    behavior of references.

    --
    Neil Cerutti <>
    Neil Cerutti, Jan 16, 2008
    #1
    1. Advertising

  2. Neil Cerutti

    Guest

    On Jan 16, 1:21 pm, "Neil Cerutti" <> wrote:
    > On Jan 15, 2008 10:09 PM, J. Peng <> wrote:
    >
    > > Hello,

    >
    > > I saw this statement in Core Python Programming book,

    >
    > > All arguments of function calls are made by reference, meaning that
    > > any changes to these parameters within the function
    > > affect the original objects in the calling function.

    >
    > Yes, that's generally correct. But you must be careful about what is
    > meant by "changes to parameters". Assigning a new value to a parameter
    > name (inside the function, a parameter is just a local variable) does
    > not change the original object--it only rebinds the local variable to
    > a new object.
    >
    > In the following function, a is rebound with an assignment statement,
    > while b is mutated, i.e., changed, with an assignment statement.
    >
    > def f(a, b):
    > a = 12
    > b.value = 14
    >
    > Argument a will never be changed, while argument b will be. Python's
    > argument passing semantics are extremely simple. It's the assignment
    > statement that's tricky: some assignments mutate/change objects, and
    > some only rebind names.
    >
    > > Does this mean there is not pass-values calling to a function in
    > > python? only pass-reference calling? Thanks!

    >
    > Neither is quite true. Values are passed by binding parameter names to
    > their corresponding arguments. This is similar to pass-by-reference in
    > some cases (when the argument is mutated) but not in others (when the
    > argument is not mutated). Thinking of it as pass-by-reference may help
    > you to understand it, but bear in mind that Python's "references" may
    > be rebound to new objects, which is quite different from the usual
    > behavior of references.
    >
    > --
    > Neil Cerutti <>


    So basically the scope is the reason for confusion a lot of the time?

    def some_name():
    alist = [5]
    bint = 5
    cstring = '5'
    ddictionary = {0:5}

    def other_name(alist, bint, cstring, ddictionary):
    alist = 4
    bint = 4
    cstring = '4'
    ddictionary = 4
    print "other_name:",
    print alist, bint, cstring, ddictionary

    def another_name(alist, bint, cstring, ddictionary):
    alist[0] = 3
    # bint cannot be changed it is immutable
    # cstring cannot be changed it is immutable
    ddictionary[0] = 3
    print "another_name:",
    print alist, bint, cstring, ddictionary

    another_name(alist, bint, cstring, ddictionary)
    other_name(alist, bint, cstring, ddictionary)
    print "our entries are now:",
    print alist, bint, cstring, ddictionary
    , Jan 16, 2008
    #2
    1. Advertising

  3. Neil Cerutti

    Neil Cerutti Guest

    On Jan 16, 2008 7:58 AM, <> wrote:
    > On Jan 16, 1:21 pm, "Neil Cerutti" <> wrote:
    > > In the following function, a is rebound with an assignment statement,
    > > while b is mutated, i.e., changed, with an assignment statement.
    > >
    > > def f(a, b):
    > > a = 12
    > > b.value = 14
    > >
    > > Argument a will never be changed, while argument b will be. Python's
    > > argument passing semantics are extremely simple. It's the assignment
    > > statement that's tricky: some assignments mutate/change objects, and
    > > some only rebind names.

    >
    > So basically the scope is the reason for confusion a lot of the time?


    No, my hypothesis is that Python's assignment statement semantics are
    the tricky part--once you understand them, the utter simplicity of
    Python's argument passing semantics will be evident.

    --
    Neil Cerutti <>
    Neil Cerutti, Jan 16, 2008
    #3
  4. Neil Cerutti

    Guest

    >
    > No, my hypothesis is that Python's assignment statement semantics are
    > the tricky part--once you understand them, the utter simplicity of
    > Python's argument passing semantics will be evident.


    Indeed, I find the simple nature of it and the fact things tend not to
    change is extremely useful. It is an advantage to have rules to abide
    by that cannot be ignored or by-passed.
    , Jan 16, 2008
    #4
    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. Jerry
    Replies:
    20
    Views:
    7,926
    Roedy Green
    Sep 9, 2005
  2. blufox
    Replies:
    2
    Views:
    550
  3. Mr A
    Replies:
    111
    Views:
    2,107
  4. Radde
    Replies:
    38
    Views:
    995
  5. J. Peng

    no pass-values calling?

    J. Peng, Jan 16, 2008, in forum: Python
    Replies:
    19
    Views:
    448
    Dennis Lee Bieber
    Jan 16, 2008
Loading...

Share This Page