Getting references to obect instances into a list

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Wed Aug 27 11:39:30 EDT 2008


On Wed, 27 Aug 2008 08:32:52 -0700, parent.eric.3 at gmail.com wrote:

> Thanks for your reply Simon.
> 
> I will read the article you told me to but first, please, have a look at
> this snippet:

Please read the article!

>>>> 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?

Because neither the name `q` nor the list object bound to it has anything 
to do with the name `p`.  ``del`` does not delete objects but *names*.  
Objects exist as long as there is a reference to them.  You deleted the 
name `p` and thus one reference to the list with the three characters but 
there's still the reference from the list bound to `q` to that three 
character list.

What did you expect for an "updated q" anyway?

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list