0% found this document useful (0 votes)
2 views9 pages

Day 3

The document covers the basics of Python bytecode and its compilation process, explaining how Python converts human-readable code into bytecode before execution. It introduces the `dis` module for viewing bytecode and discusses the internals of CPython, the standard Python implementation. By the end of the session, participants will understand the steps involved in running Python code and the significance of bytecode.

Uploaded by

radhikagawali102
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Day 3

The document covers the basics of Python bytecode and its compilation process, explaining how Python converts human-readable code into bytecode before execution. It introduces the `dis` module for viewing bytecode and discusses the internals of CPython, the standard Python implementation. By the end of the session, participants will understand the steps involved in running Python code and the significance of bytecode.

Uploaded by

radhikagawali102
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Python Basics

Bytecode & Compilation


Agenda – Python Basics - Day 3

• What is Bytecode?
• How Python runs code
• Using the dis module
• How CPython works
• Simple code demo.
• Q&A
Learning Objectives

By the end of this session, you will understand:


• What happens when you run Python code
• How Python converts code into bytecode
• How to view bytecode using dis
• What CPython is and why it matters
What is Python Bytecode?

Technical Definition
• Think of bytecode as Python's "secret language" - it's the
intermediate code that Python creates from your human-readable
Python code before actually running it.
• Bytecode is a set of instructions Python creates from your code.
• It's a middle step between code you write and code that runs.
• Saved as .pyc files in __pycache__.
• Bytecode is a low-level, platform-independent representation of
your Python source code that consists of instruction sequences for
the Python Virtual Machine (PVM)
Python's dis Module

• The `dis` module is Python's built-in disassembler that lets you peek under the
hood and see the bytecode instructions that Python generates from your code.
• dis shows bytecode instructions for a function
• The dis module provides functions to analyze CPython bytecode by
disassembling functions, methods, classes, and code objects into human-
readable instruction sequences.
• It reveals the stack-based operations that the CPython interpreter performs.
CPython Internals

• CPython is the standard Python implementation. It's called


"CPython" because it's written in C. Understanding its internals
helps you write better Python code.
• CPython is the default Python interpreter.
• Your code goes through these steps:
.py file --> AST (Syntax Tree) --> Bytecode --> Python VM
• Python compiles code to bytecode, then the VM runs it.
Disassemble Function

import dis
def greet(name):
return "Hi " + name
dis.dis(greet)
How Python Runs Code

• You write a .py file.


• Python compiles it into bytecode.
• It stores the bytecode in __pycache__.
• Python executes bytecode in the virtual machine.
Q&A

THANK YOU

You might also like