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

  • Home
  • SEARCH
  • 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 533017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:27:55+00:00 2026-05-13T09:27:55+00:00

I had always assumed that the Python interpreter did no optimizations without a -O

  • 0

I had always assumed that the Python interpreter did no optimizations without a -O flag, but the following is a bit strange:

>>> def foo():
...     print '%s' % 'Hello world'
...
>>> from dis import dis 
>>> dis(foo)
  2           0 LOAD_CONST               3 ('Hello world')
              3 PRINT_ITEM          
              4 PRINT_NEWLINE       
              5 LOAD_CONST               0 (None)
              8 RETURN_VALUE        

It appears as though the interpreter is doing some folding on the modulo of two string constants. If I add a variable in though, it gives an unoptimized result:

>>> def foo():
...     s = 'Hello world!'
...     print '%s' % s
... 
>>> dis(foo)
  2           0 LOAD_CONST               1 ('Hello world!')
              3 STORE_FAST               0 (s)

  3           6 LOAD_CONST               2 ('%s')
              9 LOAD_FAST                0 (s)
             12 BINARY_MODULO       
             13 PRINT_ITEM          
             14 PRINT_NEWLINE       
             15 LOAD_CONST               0 (None)
             18 RETURN_VALUE        

What optimizations does Python do without the -O flag? And is there any way to disable them? I’d like to see how unoptimized Python bytecode will look. I don’t plan on doing this in any production type environment.

  • 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-13T09:27:55+00:00Added an answer on May 13, 2026 at 9:27 am

    Yep, it does do constant folding, here’s a simpler example:

    >>> def f(): return 23+100
    ... 
    >>> dis.dis(f)
      1           0 LOAD_CONST               3 (123)
                  3 RETURN_VALUE        
    >>> 
    

    No way to block this (except by changing sources) AFAIK.

    Edit: for all the optimization flow, see peephole.c — this is also probably the handiest place to “change sources”, e.g. change line 320 from

    if (codelen > 32700)
    

    to

    if (codelen > 0)
    

    to make sure all optimizations are unconditionally disabled.

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

Sidebar

Ask A Question

Stats

  • Questions 291k
  • Answers 291k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If your data is for a two color image...black and… May 13, 2026 at 6:04 pm
  • Editorial Team
    Editorial Team added an answer The implementation of JNI functions follows the scoping rules for… May 13, 2026 at 6:04 pm
  • Editorial Team
    Editorial Team added an answer http://initd.org/psycopg/docs/usage.html#thread-safety May 13, 2026 at 6:04 pm

Related Questions

Update: This is, as I was told, no principle Python related problem, but seems
I had always assumed that the first entry in sys.path by default was the
This is a question that was sparked by Rob Walker 's answer here .
I had always assumed that a file would leak if it was opened without
I always assumed that if I was using Select(x=> ...) in the context of

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.