[issue14707] extend() puzzled me.

Ezio Melotti report at bugs.python.org
Wed May 2 18:25:36 CEST 2012


Ezio Melotti <ezio.melotti at gmail.com> added the comment:

>>> a = ['1']
>>> b = []
>>> c = a
# now c and a refer to the same object
>>> b.append(a)
# this object is appended to b
>>> a
['1']
>>> b
[['1']]
>>> c
['1']
# a and c refer to the same object you have in b
# so all these ['1'] are actually the same object
>>> a = ['2']
# now a refers to another object
>>> c.extend(a)
# and here you are extending the object referred by c
# and the same object that you have in b
>>> b
[['1', '2']]
>>> c
['1', '2']
# so this is correct
>>> a
['2']
>>>

You can use id() to verify the identity of the objects, and read http://python.net/crew/mwh/hacks/objectthink.html for more information.

----------
nosy: +ezio.melotti
stage:  -> committed/rejected

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue14707>
_______________________________________


More information about the Python-bugs-list mailing list