Q about object identity

V

vronskij

Hello,

I am testing object identity.

If I do it from the interpreter, I get strange results.
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
 
C

Christian Heimes

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
 
V

vronskij

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
 
M

Marc 'BlackJack' Rintsch

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
 
E

Ethan Furman

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.
 
T

Tommy Grav

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top