Reference?

Peter Otten __peter__ at web.de
Sat Aug 23 11:34:02 CEST 2003


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




More information about the Python-list mailing list