Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41371

Re: tuple of ids of integers or lists

References <8e66719c-5e90-4776-bba9-a11c29fba2f1@googlegroups.com> <roy-0C67C8.15591717032013@70-1-84-166.pools.spcsdns.net> <51a8f122-82a2-4eb0-9b8f-50d011591ae1@googlegroups.com>
Date 2013-03-18 07:34 +1100
Subject Re: tuple of ids of integers or lists
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3398.1363552494.2939.python-list@python.org> (permalink)

Show all headers | View raw


On Mon, Mar 18, 2013 at 7:23 AM,  <bartolome.sintes@gmail.com> wrote:
> 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):

>>> id([3])
15597160
>>> _
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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

tuple of ids of integers or lists bartolome.sintes@gmail.com - 2013-03-17 12:43 -0700
  Re: tuple of ids of integers or lists Roy Smith <roy@panix.com> - 2013-03-17 15:59 -0400
    Re: tuple of ids of integers or lists bartolome.sintes@gmail.com - 2013-03-17 13:23 -0700
      Re: tuple of ids of integers or lists Chris Angelico <rosuav@gmail.com> - 2013-03-18 07:34 +1100

csiph-web