Finding the instance reference of an object
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Sat Nov 1 01:05:12 CET 2008
On Fri, 31 Oct 2008 08:42:53 -0700, pjacobi.de wrote:
> Instead of comparing integers:
>
>> 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*?
To somebody who is stuck in the traditional mentality of "call by
reference" versus "call by value", it implies that Python copies tuples
and ints (and strings, and frozensets) but not lists (and dicts and sets
and class instances).
They would be wrong, but when you start with faulty assumptions that is
often the case. The assumption that there are only two calling
conventions is such a pernicious assumption. Look at how many thousands
of words have been spent trying to get Joe to understand that c-by-r and
c-b-v are not the only two options, and that what Python does is not
either c-b-r or c-b-v.
--
Steven
More information about the Python-list
mailing list