call by reference howto????

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Thu Mar 13 17:22:30 EDT 2008


On Thu, 13 Mar 2008 08:54:43 +0000, Paul Rudin wrote:

>> Whatever python has for a calling convention, it is close enough that
>> naming it "call by reference" gives people a reasonable idea of what is
>> going on.
> 
> Quite. The thing is not to get hung up with the label - but to
> understand what actually happens.

Agreed. And I think the best way to do that is to use the same terms that 
other languages use to get very different behaviour indeed.

"Python is call by reference, but this doesn't work like call by 
reference in all the other languages I've used:

def negate(n):
    n = -n

x = 2
negate(x)
assert x == -2

Is this a bug, or is Python actually call by value?"


And now comes the explanations that Python is actually call by value, but 
the values being copied are *pointers* (ignoring Jython, IronPython and 
PyPy); or that Python is really call by reference, but different 
"things" (it's never quite clear what exactly) happen depending on 
whether the arguments are mutable or immutable; or that if you define 
reference broadly enough, everything is a reference; or that if you 
define value broadly enough, everything is a value; or if schools would 
just stop teaching kiddies C and start teaching them Lisp, call by 
reference semantics would be understood in a different way, or or or or...

And thus, by insisting that there are two and only two calling 
conventions, no matter how many different kinds of calling behaviour 
actually exist, we ensure that we'll be having this same *&$%^*# argument 
when Python 4000 comes out.

And for that we can all be grateful.



-- 
Steven



More information about the Python-list mailing list