Re: Finding the instance reference of an object
- From: Steven D'Aprano <steve@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: 01 Nov 2008 00:15:56 GMT
On Fri, 31 Oct 2008 16:02:53 +0000, Duncan Booth wrote:
pjacobi.de@xxxxxxxxxxxxxx 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
.
- Prev by Date: Re: Finding the instance reference of an object
- Next by Date: Re: split() and string.whitespace
- Previous by thread: Re: Finding the instance reference of an object
- Next by thread: Re: Finding the instance reference of an object
- Index(es):
Relevant Pages
|