On Tue, 03 Jun 2008 23:08:46 +0200, Christian Heimes wrote: > [EMAIL PROTECTED] schrieb: >> Hello, >> >> I am testing object identity. >> >> If I do it from the interpreter, I get strange results. >> >>>>> print [] is [] >> False >> >>>>> print id([]), id([]) >> 3083942700 3083942700 >> >> >> >> Why is that? Isn't this an error? > > No, it's not an error. You are getting this result because the list > implementation keeps a bunch of unused list objects in a free list. It's > an optimization trick. Python has to create two different list objects > for "[] is []" while it can reuse the same list object for id([]) == id([]).
I don't think you need optimization tricks for the explanation. In ``[] is []`` there need to be two lists that exist at the same time to be compared by the ``is`` operator. With ``id([]) == id([])`` just the id numbers have to exist at the same time, so the execution may look like this: • create empty list • call `id()` with it • remember first id number • when `id()` returns, the list is not referenced anymore and can be garbage collected • create empty list, and here the list object might get allocated at the same memory location as the first empty list → same id. • call `id()` with it • remember second id number • garbage collect second empty list • compare both numbers Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list