function arguments passed by reference

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marcello Pietrobon

    function arguments passed by reference

    Hello,

    My background is C++

    I would like to know for sure if it is possible to pass a value by
    reference to a function AND having it changed by the function itself


    def changeValue( text ):
    text = 'c'
    print text
    return
    [color=blue][color=green][color=darkred]
    >>> txt = 'a'
    >>>changeValu e( txt )[/color][/color][/color]
    'c'[color=blue][color=green][color=darkred]
    >>>print txt[/color][/color][/color]
    'a'

    Question 1)
    I guess that in Python ( differently than in C++) somehow txt is passed
    by reference even if its value is not changed.
    Is this true ?

    Question 2)
    How can I define a function that changes the value of txt ( without
    having to return the changed value ) ?

    Question 3)
    Is there some documentation talking extensively about this ?


    Thank you,
    Marcello



  • John Roth

    #2
    Re: function arguments passed by reference


    "Marcello Pietrobon" <teiffel@attglo bal.net> wrote in message
    news:mailman.8. 1078344338.736. python-list@python.org ...[color=blue]
    > Hello,
    >
    > My background is C++
    >
    > I would like to know for sure if it is possible to pass a value by
    > reference to a function AND having it changed by the function itself
    >
    >
    > def changeValue( text ):
    > text = 'c'
    > print text
    > return
    >[color=green][color=darkred]
    > >>> txt = 'a'
    > >>>changeValu e( txt )[/color][/color]
    > 'c'[color=green][color=darkred]
    > >>>print txt[/color][/color]
    > 'a'
    >
    > Question 1)
    > I guess that in Python ( differently than in C++) somehow txt is passed
    > by reference even if its value is not changed.
    > Is this true ?
    >
    > Question 2)
    > How can I define a function that changes the value of txt ( without
    > having to return the changed value ) ?
    >
    > Question 3)
    > Is there some documentation talking extensively about this ?[/color]

    The key concept is that everything is an object, and all objects
    are passed by reference in the sense that what's bound to the
    funtion's parameters is the objects that were passed in.

    However, you cannot affect the bindings in the calling environment.
    Regardless of what you do (unless you do some deep magic with
    the invocation stack) nothing is going to affect the bindings in the
    caller.

    So you can rebind anything you want to the parameters within
    the function, and it will have no effect on the calling environment.

    On the other hand, if you mutate a mutable object that was passed
    as a parameter, that change will be visible after you return.

    For example:

    def foo(bar):
    bar = "shazoom"

    a = "shazam"
    foo(a)

    The result is that a is still "shazam."

    However, if you do this:

    def foo(bar):
    bar.append("sha zoom")

    a = ["shazam"]
    foo(a)

    the result is:

    ["shazam", "shazoom"]

    HTH
    John Roth

    "shazoom" ::= ["strength", "health", "aptitude", "zeal", "ox, power of",
    "ox, power of another", "money"]

    whatever the value you passed in remains.[color=blue]
    >
    >
    > Thank you,
    > Marcello
    >
    >
    >[/color]


    Comment

    Working...