call by reference howto????
sturlamolden
sturlamolden at yahoo.no
Sat Mar 15 11:40:06 EDT 2008
On 28 Feb, 02:24, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> Python doesn't do call by reference. Nor does it do call by value. Please
> pay no attention to anyone who says it does.
Exactly. Python pass variables the same way as Lisp, which is neither
"call-by-value" (cf. C) nor "call-by-reference" (cf. Fortran).
The Python equivalent of "pass by reference" is a function that
returns its arguments to the caller:
def foobar(arg1, arg2, arg3):
# Mutate an object shared with the caller.
# I.e. calling convention cannot be not "pass-by-value"
arg1.whatever = 1
# Rebind variables in the local namespace.
# I.e. calling convention cannot be "pass-by-reference"
arg1, arg2, arg3 = 1, 2, 3
# Return rebound variables to achieve the effect of
# "pass-by-reference":
return (arg1, arg2, arg3)
# Call the function foobar, and rebind its arguments to its
# return values:
arg1, arg2, arg3 = foobar(arg1, arg2, arg3)
More information about the Python-list
mailing list