no pass-values calling?

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

  1. J. Peng

    J. Peng Guest

    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.


    Does this mean there is not pass-values calling to a function in
    python? only pass-reference calling? Thanks!
    J. Peng, Jan 16, 2008
    #1
    1. Advertising

  2. On Wed, 16 Jan 2008 11:09:09 +0800, 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.
    >
    >
    > Does this mean there is not pass-values calling to a function in python?
    > only pass-reference calling? Thanks!


    No, Python does not use either pass by reference or pass by value. It
    uses pass by object. (Sometimes called "pass by object reference".)

    See: http://effbot.org/zone/call-by-object.htm

    for further details.


    --
    Steven
    Steven D'Aprano, Jan 16, 2008
    #2
    1. Advertising

  3. On Wed, 16 Jan 2008 11:09:09 +0800, "J. Peng" <>
    declaimed the following in comp.lang.python:

    >
    > All arguments of function calls are made by reference, meaning that


    Since all "variable" names in Python are references to objects,
    anything accessed using a name is accessed by reference.

    def dummy(a1, a2, etc):
    pass

    dummy("this", something, else)

    "links" by effectively doing the equivalent of

    a1 = "this"
    a2 = something
    etc = else

    > any changes to these parameters within the function
    > affect the original objects in the calling function.
    >

    The parameter names are local to the function. Any unqualified
    assignment to them is also local and switches the name to a different
    object.

    Assignment to a qualified name (those with . or [] appended) are
    "going inside" the object the name refers to and rebinding components of
    that object. Of course, this can only work if the object itself is
    mutable -- a list, dictionary, module component, or class instance
    attribute.

    alist = []
    anint = 2
    astr = "Touch me"

    dummy(alist, anint, astr)

    "dummy" can only modify the contents of the first argument -- the
    integer and string can not be mutated.
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
    Dennis Lee Bieber, Jan 16, 2008
    #3
  4. Dennis Lee Bieber wrote:
    > Since all "variable" names in Python are references to objects,
    > anything accessed using a name is accessed by reference.


    Anybody using the terms variable, reference or call-by-value is most
    likely explaining Python the wrong way.

    Sorry dude :)

    Christian
    Christian Heimes, Jan 16, 2008
    #4
  5. J. Peng

    J. Peng Guest

    On Jan 16, 2008 1:45 PM, Dennis Lee Bieber <> wrote:
    > On Wed, 16 Jan 2008 11:09:09 +0800, "J. Peng" <>
    >
    > alist = []
    > anint = 2
    > astr = "Touch me"
    >
    > dummy(alist, anint, astr)
    >
    > "dummy" can only modify the contents of the first argument -- the
    > integer and string can not be mutated.


    Hi,

    How to modify the array passed to the function? I tried something like this:

    >>> a

    [1, 2, 3]
    >>> def mytest(x):

    .... x=[4,5,6]
    ....
    >>> mytest(a)
    >>> a

    [1, 2, 3]

    As you see, a was not modified.
    Thanks!
    J. Peng, Jan 16, 2008
    #5
  6. On Wed, 16 Jan 2008 13:59:03 +0800, J. Peng wrote:

    > Hi,
    >
    > How to modify the array passed to the function? I tried something like
    > this:
    >
    >>>> a

    > [1, 2, 3]
    >>>> def mytest(x):

    > ... x=[4,5,6]



    This line does NOT modify the list [1, 2, 3]. What it does is create a
    new list, and assign it to the name "x". It doesn't change the existing
    list.


    If you have not already done this, you should read this:

    http://effbot.org/zone/python-objects.htm


    Consider this function:

    def test(alist):
    alist.append(0) # this modifies the existing list
    alist = [1, 2, 3] # this changes the name "alist"
    return alist


    Now try it:

    oldlist = [10, 9, 8, 7]
    newlist = test(oldlist)


    Can you predict what oldlist and newlist will be equal to?

    oldlist will be [10, 9, 8, 7, 0] and newlist will be [1, 2, 3]. Do you
    see why?




    --
    Steven
    Steven D'Aprano, Jan 16, 2008
    #6
  7. J. Peng

    Chris Guest

    On Jan 16, 7:59 am, "J. Peng" <> wrote:
    > On Jan 16, 2008 1:45 PM, Dennis Lee Bieber <> wrote:
    >
    > > On Wed, 16 Jan 2008 11:09:09 +0800, "J. Peng" <>

    >
    > > alist = []
    > > anint = 2
    > > astr = "Touch me"

    >
    > > dummy(alist, anint, astr)

    >
    > > "dummy" can only modify the contents of the first argument -- the
    > > integer and string can not be mutated.

    >
    > Hi,
    >
    > How to modify the array passed to the function? I tried something like this:
    >
    > >>> a

    > [1, 2, 3]
    > >>> def mytest(x):

    >
    > ... x=[4,5,6]
    > ...>>> mytest(a)
    > >>> a

    >
    > [1, 2, 3]
    >
    > As you see, a was not modified.
    > Thanks!


    'a' was not modified because you locally assigned a new object with
    'x=[4,5,6]'. If you want the new list you created you will have to
    return it. You can see how you modify it if you were to use
    'x.append()' or 'x.extend()' for eg.
    Chris, Jan 16, 2008
    #7
  8. J. Peng

    Ben Finney Guest

    Christian Heimes <> writes:

    > Dennis Lee Bieber wrote:
    > > Since all "variable" names in Python are references to objects,
    > > anything accessed using a name is accessed by reference.

    >
    > Anybody using the terms variable, reference or call-by-value is most
    > likely explaining Python the wrong way.


    The term "reference" is fine, since that's exactly how it works. One
    gets at an object via some reference, be it a name or some access into
    a container object. When an object has no more references to itself,
    it becomes a candidate for garbage collection. And so on.

    --
    \ "I planted some bird seed. A bird came up. Now I don't know |
    `\ what to feed it." -- Steven Wright |
    _o__) |
    Ben Finney
    Ben Finney, Jan 16, 2008
    #8
  9. J. Peng

    J. Peng Guest

    On Jan 16, 2008 2:30 PM, Steven D'Aprano
    <> wrote:
    > On Wed, 16 Jan 2008 13:59:03 +0800, J. Peng wrote:
    >
    > > Hi,
    > >
    > > How to modify the array passed to the function? I tried something like
    > > this:
    > >
    > >>>> a

    > > [1, 2, 3]
    > >>>> def mytest(x):

    > > ... x=[4,5,6]

    >
    >
    > This line does NOT modify the list [1, 2, 3]. What it does is create a
    > new list, and assign it to the name "x". It doesn't change the existing
    > list.
    >


    Sounds strange.
    In perl we can modify the variable's value like this way:

    $ perl -le '
    > $x=123;
    > sub test {
    > $x=456;
    > }
    > test;
    > print $x '

    456
    J. Peng, Jan 16, 2008
    #9
  10. On Wed, 16 Jan 2008 06:55:46 +0100, Christian Heimes <>
    declaimed the following in comp.lang.python:


    > Anybody using the terms variable, reference or call-by-value is most
    > likely explaining Python the wrong way.
    >
    > Sorry dude :)
    >

    At least give me credit for putting "variable" in quotes -- to imply
    that it isn't the classical "mailbox" variable (fixed address containing
    some data)

    As far as I'm concerned, there is a pool of objects, and a pool of
    names with strings of twine [vs characters <G>] (the references) to
    objects. For names, there are, in my mind, unqualified names, and
    qualified names. A qualified name is one that "goes inside" an object
    and allows changing the inside when it appears on the left of an = (or
    in absence of an =); unqualified names on the left of an = move the
    name/twine from whatever object to which it had been connected over to
    some other object.

    a = b #unqualified, "a" is disconnected from whatever, and
    #connects it to "b"
    a[1] = b #qualified, goes inside "a" to move the connection
    #the second element from whatever to "b"
    a.it = b #qualified, goes inside of "a" to move the connection
    #of the "it" component from whatever to "b"
    a.that(b) #goes inside of "a" using some method which might
    #connect something to "b"
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
    Dennis Lee Bieber, Jan 16, 2008
    #10
  11. On Wed, 16 Jan 2008 13:59:03 +0800, "J. Peng" <>
    declaimed the following in comp.lang.python:


    > How to modify the array passed to the function? I tried something like this:
    >
    > >>> a

    > [1, 2, 3]
    > >>> def mytest(x):

    > ... x=[4,5,6]


    x is unqualified (in my terms), so you have just disconnected it
    from the original argument and connected it to [4,5,6]

    But try

    x[1] = [4,5,6]
    or
    x[:] = [4,5,6]
    or
    x.append([4,5,6])
    or
    x.extend([4,5,6])

    All of these are qualified "x", meaning "go inside of the object
    that 'x' is connected to, and modify the insides}

    But note that you can not do this if you pass a string, tuple, or
    number -- those are not mutable (while string and tuple can be indexed
    to retrieve parts, you can not change parts of them).
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
    Dennis Lee Bieber, Jan 16, 2008
    #11
  12. Ben Finney wrote:
    > The term "reference" is fine, since that's exactly how it works. One
    > gets at an object via some reference, be it a name or some access into
    > a container object. When an object has no more references to itself,
    > it becomes a candidate for garbage collection. And so on.


    Thanks you, but I know exactly how Python works. I'm actually developing
    CPython and PythonDotNET. While your description of Python's memory
    management is technically, it's just an implementation detail of the
    CPython implementation. Jython and IronPython are using different
    approaches for GC.

    Anyway your message doesn't help a newbie and it gives most certainly
    the wrong impression. You are using words that have a different meaning
    in other languages. If you explain Python w/o the words variable,
    pointer, reference or call-by-value you have a much better chance to
    explain it right. Trust me :)

    Christian
    Christian Heimes, Jan 16, 2008
    #12
  13. J. Peng

    J. Peng Guest

    On Jan 16, 2008 3:03 PM, Dennis Lee Bieber <> wrote:
    > On Wed, 16 Jan 2008 13:59:03 +0800, "J. Peng" <>
    > declaimed the following in comp.lang.python:
    >
    >
    > > How to modify the array passed to the function? I tried something like this:
    > >
    > > >>> a

    > > [1, 2, 3]
    > > >>> def mytest(x):

    > > ... x=[4,5,6]

    >
    > x is unqualified (in my terms), so you have just disconnected it
    > from the original argument and connected it to [4,5,6]
    >


    Ok, thanks.
    But there is a following question,when we say,

    >>> x=[1,2,3]


    we create a list.then we say,

    >>> x=[4,5,6]


    we create a new list and assign it to x for future use.
    How to destroy the before list [1,2,3]? does python destroy it automatically?
    J. Peng, Jan 16, 2008
    #13
  14. "J. Peng" <> writes:

    > we create a new list and assign it to x for future use. How to
    > destroy the before list [1,2,3]? does python destroy it
    > automatically?


    Yes, Python detects that the older list is no longer in use (if that
    is indeed the case), and destroys it automatically. If you're curious
    how that works, take a look at
    http://en.wikipedia.org/wiki/Reference_counting
    Hrvoje Niksic, Jan 16, 2008
    #14
  15. J. Peng

    Ben Finney Guest

    Christian Heimes <> writes:

    > Ben Finney wrote:
    > > The term "reference" is fine, since that's exactly how it works.
    > > One gets at an object via some reference, be it a name or some
    > > access into a container object. When an object has no more
    > > references to itself, it becomes a candidate for garbage
    > > collection. And so on.

    >
    > Thanks you, but I know exactly how Python works. I'm actually
    > developing CPython and PythonDotNET.


    Uh, okay. I didn't ask for you to flash your credentials, but if that
    is significant to you, be my guest.

    > Anyway your message doesn't help a newbie and it gives most
    > certainly the wrong impression. You are using words that have a
    > different meaning in other languages. If you explain Python w/o the
    > words variable, pointer, reference or call-by-value you have a much
    > better chance to explain it right. Trust me :)


    I've done my share of explaining of Python to people, and found
    "reference" to be exactly the right term to help the newbie understand
    what's happening and what they should expect.

    I agree with you on "variable", "pointer", and "call-by-value". Those
    don't describe how Python works, and thus only confuse the matter.
    Thus I avoid them, and correct newbies who appear to be getting
    confused because of those existing concepts.

    --
    \ "I don't accept the currently fashionable assertion that any |
    `\ view is automatically as worthy of respect as any equal and |
    _o__) opposite view." -- Douglas Adams |
    Ben Finney
    Ben Finney, Jan 16, 2008
    #15
  16. On Wed, 16 Jan 2008 17:46:39 +1100, Ben Finney wrote:

    > Christian Heimes <> writes:
    >
    >> Dennis Lee Bieber wrote:
    >> > Since all "variable" names in Python are references to objects,
    >> > anything accessed using a name is accessed by reference.

    >>
    >> Anybody using the terms variable, reference or call-by-value is most
    >> likely explaining Python the wrong way.

    >
    > The term "reference" is fine, since that's exactly how it works. One
    > gets at an object via some reference, be it a name or some access into a
    > container object. When an object has no more references to itself, it
    > becomes a candidate for garbage collection. And so on.


    The term "reference" *by itself* is fine, so long as there is no
    possibility of confusion with the very common concept of "call by
    reference" (or "pass by reference").

    But since the Original Poster himself raised the question of whether
    Python is call by reference or call by value, that should be a great big
    warning flag to avoid the term "reference" until he's reset his brain.
    Python is not C, and if you talk about Python using C terminology without
    making it absolutely crystal clear that the semantics of that terminology
    is different in Python, then you'll just reinforce the O.P.'s
    misunderstandings.

    Python might have "references" in the generic sense, but in the specific
    sense that it is understood by most people with C/Pascal/Java/Perl
    experience, Python does not.



    --
    Steven
    Steven D'Aprano, Jan 16, 2008
    #16
  17. J. Peng a écrit :
    > On Jan 16, 2008 2:30 PM, Steven D'Aprano
    > <> wrote:
    >> On Wed, 16 Jan 2008 13:59:03 +0800, J. Peng wrote:
    >>
    >>> Hi,
    >>>
    >>> How to modify the array passed to the function? I tried something like
    >>> this:
    >>>
    >>>>>> a
    >>> [1, 2, 3]
    >>>>>> def mytest(x):
    >>> ... x=[4,5,6]

    >>
    >> This line does NOT modify the list [1, 2, 3]. What it does is create a
    >> new list, and assign it to the name "x". It doesn't change the existing
    >> list.
    >>

    >
    > Sounds strange.
    > In perl


    This is Python, not Perl. Please follow the links provided by Steven and
    read carefully.
    Bruno Desthuilliers, Jan 16, 2008
    #17
  18. Hallöchen!

    J. Peng writes:

    > On Jan 16, 2008 2:30 PM, Steven D'Aprano
    > <> wrote:
    >> On Wed, 16 Jan 2008 13:59:03 +0800, J. Peng wrote:
    >>
    >>> How to modify the array passed to the function? I tried
    >>> something like this:
    >>>
    >>>>>> a
    >>> [1, 2, 3]
    >>>>>> def mytest(x):
    >>> ... x=[4,5,6]

    >>
    >>
    >> This line does NOT modify the list [1, 2, 3]. What it does is
    >> create a new list, and assign it to the name "x". It doesn't
    >> change the existing list.

    >
    > Sounds strange.
    > In perl we can modify the variable's value like this way:
    >
    > $ perl -le '
    >> $x=123;
    >> sub test {
    >> $x=456;
    >> }
    >> test;
    >> print $x '

    > 456


    But here, it is a global name rather than a parameter. However, I
    don't speak Perl.

    Tschö,
    Torsten.

    --
    Torsten Bronger, aquisgrana, europa vetus
    Jabber ID:
    (See http://ime.webhop.org for further contact info.)
    Torsten Bronger, Jan 16, 2008
    #18
  19. J. Peng

    Mel Guest

    J. Peng wrote:

    > Sounds strange.
    > In perl we can modify the variable's value like this way:
    >
    > $ perl -le '
    >> $x=123;
    >> sub test {
    >> $x=456;
    >> }
    >> test;
    >> print $x '

    > 456


    Not all that strange. The Python equivalent is

    x=123
    sub test()
    global x
    x=456
    test()
    print x

    Python assignment works by binding an object with a name in a
    namespace. I suspect that perl does something similar, and the
    differences are in the rules about which namespace to use.

    Mel.
    Mel, Jan 16, 2008
    #19
  20. On Wed, 16 Jan 2008 15:18:52 +0800, "J. Peng" <>
    declaimed the following in comp.lang.python:

    > > > >>> a
    > > > [1, 2, 3]
    > > > >>> def mytest(x):
    > > > ... x=[4,5,6]

    > >

    >

    <snip>

    > Ok, thanks.
    > But there is a following question,when we say,
    >
    > >>> x=[1,2,3]

    >
    > we create a list.then we say,
    >
    > >>> x=[4,5,6]

    >
    > we create a new list and assign it to x for future use.
    > How to destroy the before list [1,2,3]? does python destroy it automatically?


    In this example -- no... Because the name "a" is still attached to
    it. In general though, if there is no path to access the object, then
    Python will dispose of it.
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
    Dennis Lee Bieber, Jan 16, 2008
    #20
    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,897
    Roedy Green
    Sep 9, 2005
  2. blufox
    Replies:
    2
    Views:
    540
  3. Mr A
    Replies:
    111
    Views:
    2,060
  4. Radde
    Replies:
    38
    Views:
    983
  5. Neil Cerutti

    Re: no pass-values calling?

    Neil Cerutti, Jan 16, 2008, in forum: Python
    Replies:
    3
    Views:
    228
Loading...

Share This Page