loading...

A peek inside Python — The How What and Why of Bytecode

Lachlan Eagling on April 28, 2019

In this post, we will dive head-first intro into what Python bytecode is, how to view it, and how to read and understand it. [Cover Photo by Jeppe... [Read Full]
markdown guide
 

You know what. A lot of things make sense after that. Cheers

 
 

Excellent question, as far as I know, the only way is to save your source code in a .py file and disassemble the entire file in the terminal.

E.g. Run the following command in the directory where the source file is saved.

python3 -m dis your_code.py
 

Does this bytecode physically generated? Where does this store?

 

Hi Govinda,

Apologies about the slow reply, have been very busy over the holiday period.

Yes, the bytecode is physically generated into files with the extension .pyc. These are then stored in the __pycache__ directory, these files are what the Python runtime actually executes when a program runs.

 

Hi Lachlan,

Thanks for replying. As far as I know '.pyc' create only if we import module(s). So let say I'm not import anything but write everything inside the same module, So what would happen to the 'physical byte code' or '.pyc'. Does it even create, where I can find it?

Hi Govinda,

You are right, if no modules are imported I believe the byte code is generated on the fly and not actually saved to disk at all.

 

the second code example is the same of the first one 😅

 

Looks like something is going awry loading the gists from Github when the post loads. Refreshing the page the same one loads into both randomly sometimes 🤦‍♂️.

Will update the post and hardcode the examples rather than loading from embedded gists if this keeps happening.

code of conduct - report abuse