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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:59:02+00:00 2026-06-07T12:59:02+00:00

Inspired by this question about Python caching small integers. Is it possible for the

  • 0

Inspired by this question about Python caching small integers.

Is it possible for the Python compiler to replace (0 – 6) with -6 at compile time? The code below suggests that it does not. If it is not possible, why not? I don’t think that the meaning of 0, -, or 6 can be different at run time.

If this is possible, why does CPython not do it?

# test_integers.py
def test_integers():
    print "-6 is -6 ?", -6 is -6 # True
    print "(0 - 6) is -6 ?", (0 - 6) is -6 # False

# import_test_integers.py
import test_integers
test_integers.test_integers()

My Python details in case this is very implementation-dependent:

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
[GCC 4.4.3] on linux2
  • 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-07T12:59:03+00:00Added an answer on June 7, 2026 at 12:59 pm

    First, you shouldn’t be using is to compare integer values to detect optimizations. That has nothing to do with anything, as explained in the question you linked. If you want to know what optimizations were performed on your function, use the dis module, which produces (in 2.7.2, after fixing your -1 typo):

    >>> import dis
    >>> 
    >>> def test_integers():
    ...     print "-6 is -6 ?", -6 is -6 # True
    ...     print "(0-6) is -6 ?", (0 - 6) is -6 # False
    ... 
    >>> dis.dis(test_integers)
      2           0 LOAD_CONST               1 ('-6 is -6 ?')
                  3 PRINT_ITEM          
                  4 LOAD_CONST               2 (-6)
                  7 LOAD_CONST               2 (-6)
                 10 COMPARE_OP               8 (is)
                 13 PRINT_ITEM          
                 14 PRINT_NEWLINE       
    
      3          15 LOAD_CONST               3 ('(0-6) is -6 ?')
                 18 PRINT_ITEM          
                 19 LOAD_CONST               6 (-6)
                 22 LOAD_CONST               2 (-6)
                 25 COMPARE_OP               8 (is)
                 28 PRINT_ITEM          
                 29 PRINT_NEWLINE       
                 30 LOAD_CONST               0 (None)
                 33 RETURN_VALUE        
    

    and you see that the subtraction actually was optimized away. You can see some others, too:

    >>> def f():
    ...     x = 1+2
    ...     x = 2-3
    ...     x = 3*4
    ...     x = 4/5
    ...     x = 5**6
    ... 
    >>> dis.dis(f)
      2           0 LOAD_CONST               7 (3)
                  3 STORE_FAST               0 (x)
    
      3           6 LOAD_CONST               8 (-1)
                  9 STORE_FAST               0 (x)
    
      4          12 LOAD_CONST               9 (12)
                 15 STORE_FAST               0 (x)
    
      5          18 LOAD_CONST               4 (4)
                 21 LOAD_CONST               5 (5)
                 24 BINARY_DIVIDE       
                 25 STORE_FAST               0 (x)
    
      6          28 LOAD_CONST              10 (15625)
                 31 STORE_FAST               0 (x)
                 34 LOAD_CONST               0 (None)
                 37 RETURN_VALUE        
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is inspired from Joel's Making Wrong Code Look Wrong http://www.joelonsoftware.com/articles/Wrong.html Sometimes you
Inspired by this question . Short version: Why can't the compiler figure out the
This question was inspired by Jon Skeet's question here where he asked about people
Inspired by this question : Is explicit type recursion possible in F#? type 'a
Inspired from this question , was wondering what are the possible ways to create
Inspired by this question . Suppose in C++ code I have a valid pointer
Inspired by this question How can I force GDB to disassemble? I wondered about
Somewhat inspired by this question about a graphical programming environment. I don't think that
Another question on SO inspired me to try this code in C#: class Program
Inspired by this question , I'd like to know whether there is any trick

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.