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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:42:32+00:00 2026-06-03T11:42:32+00:00

In Python (2.7.2),why does import dis dis.dis(i in (2, 3)) works as expected whereas

  • 0

In Python (2.7.2),why does

import dis
dis.dis("i in (2, 3)")

works as expected whereas

import dis
dis.dis("i in [2, 3]")

raises:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dis.py", line 45, in dis
  disassemble_string(x)
File "/usr/lib/python2.7/dis.py", line 112, in disassemble_string
  labels = findlabels(code)
File "/usr/lib/python2.7/dis.py", line 166, in findlabels
 oparg = ord(code[i]) + ord(code[i+1])*256
IndexError: string index out of range

Note that this doesn’t affect Python3.

  • 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-03T11:42:33+00:00Added an answer on June 3, 2026 at 11:42 am

    Short Answer

    In Python 2.x, the str type holds raw bytes, so dis assumes that if you pass it a string it is getting compiled bytecode. It tries to disassemble the string you pass it as bytecode and — purely due to the implementation details of Python bytecode — succeeds for i in (2,3). Obviously, though, it returns gibberish.

    In Python 3.x, the str type is for strings and the bytes types is for raw bytes, so dis can distinguish between compiled bytecode and strings — and assumes it is getting source code if it gets a string.


    Long Answer

    Here’s the thought process I followed to work this one out.

    1. I tried it on my Python (3.2):

      >>> import dis
      >>> dis.dis("i in (2,3)")  
        1           0 LOAD_NAME                0 (i)
                    3 LOAD_CONST               2 ((2, 3))
                    6 COMPARE_OP               6 (in)
                    9 RETURN_VALUE
      >>> dis.dis("i in [2,3]")
        1           0 LOAD_NAME                0 (i)
                    3 LOAD_CONST               2 ((2, 3))
                    6 COMPARE_OP               6 (in)
                    9 RETURN_VALUE
      

      Obviously, this works.

    2. I tried it on Python 2.7:

      >>> import dis
      >>> dis.dis("i in (2,3)")
                0 BUILD_MAP       26912
                3 JUMP_FORWARD    10272 (to 10278)
                6 DELETE_SLICE+0
                7 <44>
                8 DELETE_SLICE+1
                9 STORE_SLICE+1
      >>> dis.dis("i in [2,3]")
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
        File "C:\Python27\lib\dis.py", line 45, in dis
          disassemble_string(x)
        File "C:\Python27\lib\dis.py", line 112, in disassemble_string
          labels = findlabels(code)
        File "C:\Python27\lib\dis.py", line 166, in findlabels
          oparg = ord(code[i]) + ord(code[i+1])*256
      IndexError: string index out of range
      

      Aha! Notice also that the generated bytecode in Python 3.2 is what you would expect (“load i, load (2,3), test for membership, return the result”) whereas what you have got in Python 2.7 is gibberish. Clearly, dis is decompiling the string as bytecode in 2.7 but compiling it as Python in 3.2.

    3. I had a look in the source code for dis.dis. Here are the key points:

      Python 2.7:

      elif isinstance(x, str):
          disassemble_string(x)
      

      Python 3.2:

         elif isinstance(x, (bytes, bytearray)): # Raw bytecode
             _disassemble_bytes(x)
         elif isinstance(x, str):    # Source code
             _disassemble_str(x)
      

      Just for fun, let’s check this by passing the same bytes to dis in Python 3:

      >>> dis.dis("i in (2,3)".encode())
                0 BUILD_MAP       26912
                3 JUMP_FORWARD    10272 (to 10278)
                6 <50>
                7 <44>
                8 <51>
                9 <41>
      

      Aha! Gibberish! (Though note that it’s slightly different gibberish — the bytecode has changed with the Python version!)

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

Sidebar

Related Questions

How does this import work, what file does it use? import _functools In python
In Python, what exactly does import * import? Does it import __init__.py found in
Python does not print traceback messages from exceptions raised in daemon threads. For example,
r'\' in Python does not work as expected. Instead of returning a string with
Where is my pythonpath stored? When I write import sys sys.path Where does Python
In Python, import does_not_exist raises ImportError , and import exists exists.py : import does_not_exist
Why does Import[!python --version, Text] work on the commandline but not in the frontend
In Python 2.7.1, I import the random module. when I call randint() however, I
I have a very basic python script that does HTTP connection. import socket def
The Boost documentation does not specify this. When I use boost::python::import , where exactly

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.