Reference?

K

Kris Caselden

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?
 
P

Peter Otten

Kris said:
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
 
M

Michael Peuser

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top