Is it possible to pass a parameter by reference?

Discussion in 'Python' started by Anthony Liu, Feb 27, 2005.

  1. Anthony Liu

    Anthony Liu Guest

    I defined two functions, f1 and f2.

    f1 modifies the value of a variable called apple.

    I want to pass the modified value of apple to f2.

    How can I do this? I got stuck.




    __________________________________
    Do you Yahoo!?
    Yahoo! Mail - You care about security. So do we.
    http://promotions.yahoo.com/new_mail
     
    Anthony Liu, Feb 27, 2005
    #1
    1. Advertising

  2. Anthony Liu

    M.E.Farmer Guest

    Anthony Liu wrote:
    > I defined two functions, f1 and f2.
    >
    > f1 modifies the value of a variable called apple.
    >
    > I want to pass the modified value of apple to f2.
    >
    > How can I do this? I got stuck.



    Py>def f1(apple):
    .... apple += 1
    .... f2(apple)

    py>def f2(apple):
    .... print 'you have %s apples' % apple

    py>f1(4)
    'you have 5 apples'
    Read the docs this is pretty basic, it will save you time.
    If in doubt try it out.
    Use the interpreter, it is your friend.
    hth,
    M.E.Farmer
     
    M.E.Farmer, Feb 27, 2005
    #2
    1. Advertising

  3. Anthony Liu

    Tim Roberts Guest

    Anthony Liu <> wrote:
    >
    >I defined two functions, f1 and f2.
    >
    >f1 modifies the value of a variable called apple.
    >
    >I want to pass the modified value of apple to f2.
    >
    >How can I do this? I got stuck.


    It depends on the data type. Essentially, all objects are passed by
    reference. However, some objects can be modified, and some cannot. Lists
    can, strings and tuples cannot.

    This is one of the most important thing to understand about Python, in my
    opinion. There is a fundamental separation between an object and whatever
    names it is known by.

    Here's an example:

    def xxx(yyy):
    yyy[1] = 3

    zzz = [ 0, 1, 2 ]
    xxx(zzz)

    After this code runs, zzz will be [0,3,2]. Note that, while xxx is
    running, the list that was created as [0,1,2] has two names.

    However, this code will fail within xxx, because the string cannot be
    modified:
    aaa = "abcde"
    xxx(aaa)

    However, watch this:

    def xxx(yyy): # 0
    yyy = [ 3, 4, 5] # 1

    zzz = [ 0, 1, 2 ] # 2
    xxx(zzz) # 3

    After this runs, zzz will still be [0,1,2]. At line 2, a list object is
    created containing [0,1,2]. The variable name zzz is bound to that list.
    When we get to lines 3 and 0, the variable name yyy within function xxx is
    bound to that exact same object.

    However, in line 1, a NEW list object is created, containing [3,4,5]. The
    variable yyy is then bound to this NEW object, and its binding to the other
    list is lost. However, the variable zzz is still bound to [0,1,2].

    If you need to create a new object and return it to the mainline, do that:

    def xxx(yyy):
    yyy = [ 3, 4, 5 ]
    return yyy

    zzz = [ 0, 1, 2 ]
    zzz = xxx(zzz)
    --
    - Tim Roberts,
    Providenza & Boekelheide, Inc.
     
    Tim Roberts, Feb 27, 2005
    #3
  4. Anthony Liu

    Paddy Guest

    It is usually clearer to explicitely return values that are changed by
    a function and re-assign it to the same variable,

    x=something1
    a = something2
    def f1(s,t):
    # do something with t,
    # do something to s
    return s
    a = f1(a,x)

    Be aware however that you can wrap 'a' in a list for the same effect,
    (but it is not as easy to read).

    x=something1
    aa = [something2]
    def f2(ss,t):
    s= ss[0]
    # do something with t,
    # do something to s
    ss[0]=s
    f2(aa,x)
    a=aa[0]

    -- Pad.
     
    Paddy, Feb 27, 2005
    #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,952
    Roedy Green
    Sep 9, 2005
  2. Spartanicus
    Replies:
    4
    Views:
    1,378
  3. blufox
    Replies:
    2
    Views:
    565
  4. Abdul Mohsin
    Replies:
    1
    Views:
    267
  5. AzamSharp
    Replies:
    2
    Views:
    181
Loading...

Share This Page