Finding the instance reference of an object

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Oct 31 10:12:31 CET 2008


On Fri, 31 Oct 2008 20:22:23 +1300, greg wrote:

> Steven D'Aprano wrote:
> 
>> Which definition of call-by-value? Certainly not the one that most
>> people are familiar with, where c-b-v implies that calling a function
>> with a large array will copy the entire array.
> 
> But that's only true in languages where *assigning* a large array will
> also copy the entire array. 

Maybe, maybe not. To be consistent, it should be true. But how do you 
know if a language you are unfamiliar with is consistent?

In any case, to somebody only familiar with the classical value-vs-
reference model, Python seems rather inconsistent:


x = 1
y = x  # does assignment make copies?
y += 1
assert x == 1
=> succeeds, which implies that Python makes a copy when assigning

x = [1]
y = x  # does assignment make copies?
y += [1]
assert x == [1]
=> fails, which implies that Python uses references when assigning

Hence you get people who argue that Python does call-by-value for numbers 
and call-by-reference for lists -- and yes, I've seen people make that 
exact argument.

The reality is that Python isn't inconsistent, it merely seems to be if 
you assume it is some other language. Python actually treats numbers and 
lists identically. 

When people discover that Python's behaviour doesn't fall neatly into 
their preconceived notions of c-b-v and c-b-r, there are two ways of 
dealing with that:

(1) Accept that perhaps there's at least one more way of doing assignment 
and parameter passing.

(2) Or jump through hoops trying to force how Python works to somehow 
match your preconceptions.



> This does not happen in Python, therefore
> there is no reason to suppose that it will happen when passed as a
> parameter.

Of course there is a reason: we've been told that Python is call by 
value, and call by value implies that a copy is made when you pass it to 
a function. The fact that many programmers already have an idea of what 
they think c-b-v implies is what makes calling Python c-b-v such a 
pernicious mistake. If you want to call Python "call by ginger" I won't 
object, because most people have no preconceived ideas of what call by 
ginger means and therefore won't be lured into incorrect assumptions 
about Python.


> Before you can understand parameter passing, whether by-value or
> by-reference, you first have to understand how assignment works *in the
> language concerned*, not some other language you happen to know
> previously.

It's certainly true that the baggage people bring from their previous 
languages can sometimes be a serious barrier, but that especially happens 
when folk insist on using the same terminology to describe different 
things.

For example, to somebody coming to Python from Lisp or Scheme, the word 
"list" carries particular connotations. It's unfortunate that Python 
lists are not like Lisp lists, but we're stuck with that now, and besides 
there's only so many good names for a list-like array, and whatever name 
was chosen would trip up somebody. Another example, augmented assignment 
in Python trips up C programmers, because x += y looks the same but 
behaves differently than x += y in C.

There's enough difficulty with learning a new programming language 
without people adding to it by misusing terms like "call by value" to 
describe what Python does.


-- 
Steven



More information about the Python-list mailing list