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 9013477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:16:39+00:00 2026-06-16T03:16:39+00:00

Possible Duplicate: Python is operator behaves unexpectedly with integers Why (0-6) is -6 =

  • 0

Possible Duplicate:
Python “is” operator behaves unexpectedly with integers
Why (0-6) is -6 = False?

So, while playing with a bit with id (python 2.6.5), I noticed the following (shell session):

>>> a = 1
>>> id(a)
140524904
>>> b = 1
>>> id(b)
140524904

Of course, as soon as I modify one of the variables it gets assigned to a new memory address, i.e.

>>> b += 1
>>> id(b)
140524892

Is it the normal behavior to initially assign both variables that have identical values to the same memory location or just an optimization of i.e. CPython?

P.s. I spent a little time browsing around the code in parser, but couldn’t find where and how variables are allocated.

  • 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-06-16T03:16:41+00:00Added an answer on June 16, 2026 at 3:16 am

    As mentioned by glglgl, this is an implementation detail of CPython. If you look at Objects/longobject.c in the source code for CPython (e.g. version 3.3.0), you’ll find the answer to what’s happening:

    #if NSMALLNEGINTS + NSMALLPOSINTS > 0
    /* Small integers are preallocated in this array so that they
       can be shared.
       The integers that are preallocated are those in the range
       -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
    */
    static PyLongObject small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
    

    This explains why, after a = 1; b = 1, a is b will be True, even when you say a += 2; b +=2; a -= 2; b -= 2. Whenever a number is calculated to have a value that fits in this array, the resulting object is simply picked from this array instead, saving a bit of memory.

    You can figure out the bounds of this small_ints array using a function like this:

    def binary_search(predicate, lo, hi):
        while lo + 1 < hi:
            mid = (lo + hi) / 2
            if predicate(mid):
                lo = mid
            else:
                hi = mid
        return lo
    
    def is_small_int(n):
        p = n + 1
        q = n + 1
        return (p - 1) is (q - 1)
    
    def min_neg_small_int():
        p, q = -1, -1
        if p is not q:
            return 0
        while p is q:
            p += p
            q += q
        return binary_search(is_small_int, p / 2, p) - 1
    
    def max_pos_small_int():
        p, q = 1, 1
        if p is not q:
            return 0
        while p is q:
            p += p
            q += q
        return binary_search(is_small_int, p / 2, p)
    
    def small_int_bounds():
        return (min_neg_small_int(), max_pos_small_int())
    

    For my build (Python 2.7, 64-bit Windows build), small_int_bounds() == (-5, 256). This means that numbers between -5 and 256 (inclusive) are shared through the small_ints array in Objects/longobject.c.

    -edit- I see elssar noted that there is a similar answer about interning of some literals. This fact is also mentioned in the documentation for PyInt_FromLong, as mentioned by this answer.

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

Sidebar

Related Questions

Possible Duplicate: Python is operator behaves unexpectedly with integers I stumbled upon the following
Possible Duplicate: Python “is” operator behaves unexpectedly with integers I'm learning Python, and am
Possible Duplicate: Python “is” operator behaves unexpectedly with integers Why (0-6) is -6 =
Possible Duplicate: Python “is” operator behaves unexpectedly with integers Usually I use the type(x)
Possible Duplicate: Python Ternary Operator If Python would support the (x ? a :
Possible Duplicate: Ternary conditional operator in Python I have this problem and have no
Possible Duplicate: Python Ternary Operator Does Python have an equivalent of the ternary operator?:
Possible Duplicate: Python: Once and for all. What does the Star operator mean in
Possible Duplicate: What does the ** operator do in Python? What does the **
Possible Duplicate: Python Check if all of the following items is in a list

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.