Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5948573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:07:36+00:00 2026-05-22T17:07:36+00:00

I was fiddling around with id recently and realized that (c?)Python does something quite

  • 0

I was fiddling around with id recently and realized that (c?)Python does something quite sensible: it ensures that small ints always have the same id.

>>> a, b, c, d, e = 1, 2, 3, 4, 5
>>> f, g, h, i, j = 1, 2, 3, 4, 5
>>> [id(x) == id(y) for x, y in zip([a, b, c, d, e], [f, g, h, i, j])]
[True, True, True, True, True]

But then it occurred to me to wonder whether the same is true for the results of mathematical operations. Turns out it is:

>>> nines = [(x + y, 9) for x, y in enumerate(reversed(range(10)))]
>>> [id(x) == id(y) for x, y in nines]
[True, True, True, True, True, True, True, True, True, True]

Seems like it starts failing at n=257…

>>> a, b = 200 + 56, 256
>>> id(a) == id(b)
True
>>> a, b = 200 + 57, 257
>>> id(a) == id(b)
False

But sometimes it still works even with larger numbers:

>>> [id(2 * x + y) == id(300 + x) for x, y in enumerate(reversed(range(301)))][:10]
[True, True, True, True, True, True, True, True, True, True]

What’s going on here? How does python do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:07:37+00:00Added an answer on May 22, 2026 at 5:07 pm

    Python keeps a pool of int objects in certain numbers. When you create one in that range, you actually get a reference to the pre-existing one. I suspect this is for optimization reasons.

    For numbers outside the range of that pool, you appear to get back a new object whenever you try to make one.

    $ python
    Python 3.2 (r32:88445, Apr 15 2011, 11:09:05) 
    [GCC 4.5.2 20110127 (prerelease)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> x = 300
    >>> id(x)
    140570345270544
    >>> id(100+200)
    140570372179568
    >>> id(x*2)
    140570345270512
    >>> id(600)
    140570345270576
    

    Source

    PyObject* PyInt_FromLong(long ival)
    Return value: New reference. Create a
    new integer object with a value of
    ival.

    The current implementation keeps an
    array of integer objects for all
    integers between -5 and 256, when you
    create an int in that range you
    actually just get back a reference to
    the existing object.
    So it should be
    possible to change the value of 1. I
    suspect the behaviour of Python in
    this case is undefined. 🙂

    emphasis mine

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been fiddling around with arrays and array ordering recently and stumbled across something
I am fiddling around with .htaccess and mod_rewrite. I have a site that has
I've been fiddling around with drupal theming quite succesfully (or at least, that's what
Remember, this is using python. Well, I was fiddling around with an app I
I recently started fiddling around with as3, which look pretty good, problem is I
After fiddling around with an issue I am having I have come to this
I'm fiddling around with a small problem. In a C#-program, I've a label whose
I am fiddling around with a dialog that pops up and displays an alarm
I have this, and i've been fiddling around with it for a while but
I'm just fiddling around with user scripts in chrome right now, so please bear

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.