Python Byte code:
- Byte code of Python is generated after compiling a python program.
- Python Byte code goes through two stages.
- It compiles the code into .pyc files that is actually a bytecode. This .pyc file (bytecode) is interpreted by using CPython interpreter.
- Its use for fast execution.
Python is a dynamic language, and running it from the command line basically triggers the given steps
-
- The source is compiled first time it is encountered e.g., imported as a module or directly executed. This step generates a binary file, with a .pyc or .pyo extension depending on the system.
- The interpreter reads the binary file and executes the opcodes one at a time.
- The python interpreter is stack-based and once source is compiled corresponding .pyc is ready in the matching directory (in case of python 2.x). With python 3.x a new __pycache__ directory is produced for the source and all compiled Bytecode files are located underneath.
Example:
- Save the file as ‘MyProgram.py’. This ‘MyProgram.py’ is source code. Now if you compile this file you would get a file with ‘.pyc’ extention which is known as byte code.