Getting references to obect instances into a list

Simon Brunning simon at brunningonline.net
Wed Aug 27 17:43:02 CEST 2008


2008/8/27 parent.eric.3 at gmail.com <parent.eric.3 at gmail.com>:
> I will read the article you told me to but first, please, have a look
> at this snippet:
>
>>>> m = [2,3,4]
>>>> p = ['a','b','c']
>>>> q = [m,p]
>>>> q
> [[2, 3, 4, 'a', 'b', 'c'], ['a', 'b', 'c']]
>>>> del p
>>>> q
> [[2, 3, 4, 'a', 'b', 'c'], ['a', 'b', 'c']]
>>>>
>
>
> How come q is not updated after I deleted p?

You deleted the *name* "p". The object that it was referring too, the
list, still has a live reference - it's an element of q - so it sticks
around. It'll only go away once the last reference to it goes.

Please, read the article. ;-)

-- 
Cheers,
Simon B.
simon at brunningonline.net
http://www.brunningonline.net/simon/blog/



More information about the Python-list mailing list