nachtblume
nachtblume

Reputation: 77

How to get python bytecode?

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')

Upvotes: 1

Views: 1315

Answers (1)

OTheDev
OTheDev

Reputation: 2967

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

Upvotes: 7

Related Questions