1

I've tried using py_compile to get me some bytecode examples (for learning needs).

However, this is what I get

file.py:

print("hello world")

file.pyc:

 U

��Ob   �                   @   s   e d � dS )zhello worldN)�print� r   r   �test.py�<module>   �    

I guess I'm doing something terribly wrong, but I have no clue.

My base script is

import py_compile

py_compile.compile('test.py')
7
  • 4
    You're printing binary data, so that's what it's supposed to look like... What else did you expect exactly? Commented Apr 8, 2022 at 8:34
  • docs.python.org/3/library/dis.html Commented Apr 8, 2022 at 8:34
  • 2
    Welcome to Stack Overflow. "I guess im doing something terribly wrong, but i have no clue." What do you think it should look like instead? Why? Also, what exactly are these "learning needs"? Also, you can get Python to compile any file by simply importing it as a module. Commented Apr 8, 2022 at 8:34
  • 1
    Did you expect to see the instruction names instead of bytecode? Commented Apr 8, 2022 at 8:34
  • @Thierry Lathuille Ty for all replies, i was away for a while, sorry. Main idea was to see by my own eyes the difference between lists and tuples for example. And i've read in Mark Lutc's book about the bytecode and i was sure that bytecode and binary data are different things. LIke bytecode goes to PVM and becomes binary. DIS is smething that is really what i was hoping to see. Thanks! Commented Apr 8, 2022 at 12:42

1 Answer 1

7

Are you looking for (the) dis module (documentation)?

Example:

>>> import dis
>>> def foo(a):
...   return a * a
... 
>>> dis.dis(foo)
  2           0 LOAD_FAST                0 (a)
              2 LOAD_FAST                0 (a)
              4 BINARY_MULTIPLY
              6 RETURN_VALUE
Sign up to request clarification or add additional context in comments.

1 Comment

Some words about the relation between raw bytecode and the disassembled mnemonics would make this answer even better. The questioner and future readers seem to have difficulties to distinguish between both.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.