tuple of ids of integers or lists

B

bartolome.sintes

R

Roy Smith

In Python 3.3 for Windows, every list gets a different id when it is created:
id([3]) 46555784
id([3]) 47920192
id([4])
46532048

But if I write a tuple asking for the ids of two lists, each list seems to
get the same id:
(43079000, 43079000)

I was expecting a tuple with two different ids. What is the reason for this
behaviour?

This is a classic little gotcha.

Object ids are guaranteed to be unique, but only for the lifetime of an
object. Or, to put it another way, it is guaranteed that no two objects
that exist at the same time can have the same id.

What's happening when you do:

Is that a list, [3], is created, its id is taken, and then the list is
destroyed. In this particular implementation, object ids happen to be
the address of the object in memory. Then, a new list, [4], is created.
It just so happens that it gets created using the same memory block that
was just freed by the destruction of the other list, so it ends up in
the same place in memory. So it gets the same id!

Kind of neat, huh?

PS -- this makes a nice interview question to see if somebody really
understands what's happening beneath the surface.
 
B

bartolome.sintes

OK. Now I understand it.

I was confused because when two list are created in two different lines, Python gives them different ids, but when the two lists are created in the same line (in a tuple) Python gives them the same id. It doesn't really matter as these lists are just created and destroyed, as you have said.

Thank you very much for your fast answer.
 
C

Chris Angelico

OK. Now I understand it.

I was confused because when two list are created in two different lines, Python gives them different ids, but when the two lists are created in the same line (in a tuple) Python gives them the same id. It doesn't really matter as these lists are just created and destroyed, as you have said.

Thank you very much for your fast answer.

Here's something that'll either help you understand another aspect of
Python, or totally do your head in.
print(id([3])) 14485504
print(id([3]))
14485504

If you print it to stdout, the id can be reused! How is this?

Here's what's going on. In interactive mode, the result of the last
expression is made available under the name _ (a single underscore):
15597160

Integers are objects, like everything else. (Some of them are
pre-made, but big numbers like this aren't - at least, not in this
Python. But that's implementation-dependent too.) The retention of
this integer object can in some circumstances change where the next
object is stored. So you may find that some things behave differently
in interactive mode than in a script; or perhaps printing something
out instead of letting the interpreter display it will change what
happens.

You're looking into some extremely unspecified behaviour, here, so
anything is allowed to affect it. Which means you might be able to
learn all sorts of things about Python's internals! :)

ChrisA
 

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,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top