Finding the instance reference of an object

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Oct 31 20:15:56 EDT 2008


On Fri, 31 Oct 2008 16:02:53 +0000, Duncan Booth wrote:

> pjacobi.de at googlemail.com wrote:
> 
>>> x = 1
>>> y = x  # does assignment make copies? y += 1
>>> assert x == 1
>>> => succeeds, which implies that Python makes a copy when assigning
>> 
>> with lists:
>> 
>>> x = [1]
>>> y = x  # does assignment make copies? y += [1]
>>> assert x == [1]
>>> => fails, which implies that Python uses references when assigning
>> 
>> Compare lists with tupels:
>> 
>> x = (1,)
>> y = x  # does assignment make copies? y += (1,)
>> assert x == (1,)
>>=> succeeds, which implies *what*?
> 
> All any of this does is 'implies that += may create a new object or may
> mutate an existing object. RTFM: Python Reference Manual 6.3.1

The exact test isn't important. If you don't like those tests, replace 
them with something else: y = y + [1] perhaps, or y.sort(), or whatever 
you like. Naturally you will get different results according to whatever 
specific test you try, and the interpretation of those results will 
therefore be different. But no matter what tests are done, somebody who 
fails to understand Python's calling model (or if you prefer, its 
assignment model) will wrongly interpret the results they see in terms of 
a model they do understand.

Because call by reference and call by value are such older and 
established models, and used in such historically popular languages like 
C and Pascal, they are the most likely incorrect assumptions people will 
start from.


-- 
Steven



More information about the Python-list mailing list