Reference?

Discussion in 'Python' started by Kris Caselden, Aug 23, 2003.

  1. I've searched all the Python docs I could find, but I haven't seen any
    mention of referencing function arguments, such as you would with the
    & in c/c++. Is this possible in Python?
     
    Kris Caselden, Aug 23, 2003
    #1
    1. Advertising

  2. Kris Caselden

    mackstann Guest

    On Sat, Aug 23, 2003 at 01:57:33AM -0700, Kris Caselden wrote:
    > I've searched all the Python docs I could find, but I haven't seen any
    > mention of referencing function arguments, such as you would with the
    > & in c/c++. Is this possible in Python?


    Python is pass-by-reference by default.

    >>> f = []
    >>> def foo(n):

    .... n.append("hi")
    ....
    >>> foo(f)
    >>> print f

    ['hi']

    --
    m a c k s t a n n mack @ incise.org http://incise.org
    A bore is someone who persists in holding his own views after we have
    enlightened him with ours.
     
    mackstann, Aug 23, 2003
    #2
    1. Advertising

  3. Kris Caselden

    Peter Otten Guest

    Kris Caselden wrote:

    > I've searched all the Python docs I could find, but I haven't seen any
    > mention of referencing function arguments, such as you would with the
    > & in c/c++. Is this possible in Python?


    The analog of the Python way of handling args among the C-style languages is
    Java. So, no, you are on mission impossible :)

    class Mutable:
    pass

    v1 = Mutable()
    v1.name = "v1"
    v2 = "v2" #strings are immutable

    def fun(a1, a2):
    a1.name = "a1"
    a2 = "a2"
    fun(v1, v2)

    print v1.name # prints a1
    print v2 # prints v2

    As a workaround, you can do:

    def fun2():
    a1 = Mutable()
    a1.name = "A1"
    return a1, "A2"

    v1, v2 = fun2()

    print v1.name # prints A1
    print v2 # prints A2

    See the tutorial (http://www.python.org/doc/current/tut/node6.html) for the
    tricks you *can* do with function arguments

    Peter
     
    Peter Otten, Aug 23, 2003
    #3
  4. "Kris Caselden" <> schrieb im Newsbeitrag
    news:...

    > I've searched all the Python docs I could find, but I haven't seen any
    > mention of referencing function arguments, such as you would with the
    > & in c/c++. Is this possible in Python?


    Your question is probably not as sophisticated: Parameter passing in Python
    is *always* like & in C++.

    There had been an exaustive disyussion some weeks ago: here is my
    contribution:

    Sent: Wednesday, August 13, 2003 9:12 AM
    Subject: Re: two quick questions


    > The following examples might clear the more theoretical elaborations .....
    >
    >
    > def noUse(a):
    > a=(4,5,6)
    >
    > def tricky(a):
    > a[0]=(7,8,9)
    >
    > # case 1
    > x=[1,2,3]
    > print x
    > tricky(x)
    >
    > x=(1,2,3)
    > # case 2
    > noUse(x)
    > print x
    >
    > # case 3
    > tricky([x])
    > print x
    >
    > # case 4
    > y=[x]
    > tricky (y)
    > print x
    > print y[0]
    >
    > # case 5
    > tricky(x)
    > print x
    >
    >
    > Kindly
    > Michael Peuser
     
    Michael Peuser, Aug 23, 2003
    #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. Darren
    Replies:
    0
    Views:
    521
    Darren
    Oct 11, 2004
  2. Dude
    Replies:
    0
    Views:
    380
  3. ce
    Replies:
    1
    Views:
    3,021
  4. sam pal
    Replies:
    3
    Views:
    593
    E. Robert Tisdale
    Jul 16, 2003
  5. Craig Nicol
    Replies:
    2
    Views:
    481
    Craig Nicol
    May 10, 2004
Loading...

Share This Page