By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,850 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

Q about object identity

P: n/a
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?
If I test it in a script, all is OK.

#!/usr/bin/python

a = []
b = []

print a == b
print a is b

print id(a), id(b)

print id(None), id(None)
print id(True), id(True)
On my computer, I got these results:

True
False
3083769036 3083793516
135553336 135553336
135526444 135526444

All as expected.
jan bodnar
Jun 27 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
vr******@gmail.com 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([]).

Christian

Jun 27 '08 #2

P: n/a
On 3. Jn, 23:08 h., Christian Heimes <li...@cheimes.dewrote:
vrons...@gmail.com 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([]).

Christian
Aha.

Thanks.

jan bodnar
Jun 27 '08 #3

P: n/a
On Tue, 03 Jun 2008 23:08:46 +0200, Christian Heimes wrote:
vr******@gmail.com 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
Jun 27 '08 #4

P: n/a
vr******@gmail.com wrote:
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?
If I test it in a script, all is OK.

#!/usr/bin/python

a = []
b = []

print a == b
*print a is b*

print id(a), id(b)

print id(None), id(None)
print id(True), id(True)
On my computer, I got these results:

True
*False*
3083769036 3083793516
135553336 135553336
135526444 135526444

All as expected.
jan bodnar
Assuming I'm not missing something obvious here, the results from the
script are the same as those from the interpreter -- the _is_ returns
False in both cases.
Jun 27 '08 #5

P: n/a

On Jun 6, 2008, at 4:27 PM, Ethan Furman wrote:
vr******@gmail.com wrote:
>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?
in the first statement the two []'s are in memory at the same
time and have different id() signatures. For the second statement
the first id([]) is evaluated and release from memory and then
the second id([]) is evaluated and release from memory. Since
only one of them exist at a time they can have the same id()
signature.

Cheers
Tommy
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.