Free study guide

50 Advanced Python Concepts

I've done over 25 Python interviews for roles up to $200k in total comp. These are the concepts that kept coming up at the senior level, with a focus on performance and memory management.

Some of them look trivial. In an interview you still have to explain them clearly. Each one below has my notes, the example code from the video, and a link to the moment I cover it.

01Arrays (Python array module)

Watch at 0:53

This one actually threw me off in a real interview.

Python arrays are not the same as lists. Python has a built-in array module that lets you create type-specific arrays.

You have to specify a type code when you create them, which means every element in the array must be the same type.

Why arrays matter:

  • They are more memory-efficient than lists
  • They enforce type consistency
  • They’re faster when working with large datasets of the same type

Trade-offs:

  • Can only store one type
  • Less flexible than lists
  • Not dynamically typed like lists

Use arrays when:

  • You care about memory
  • You’re working with large numeric data
  • You don’t need list flexibility
1.arrays.pyGitHub ↗
from array import array

numbers = array('i', [1, 2, 3, 4, 5])
# i for integers

numbers.append(6)    # Add element to end
numbers.extend([7, 8])  # Add multiple elements
numbers.insert(0, 0)    # Insert at specific position

print(numbers)

02Garbage Collection & Circular References

Watch at 1:54

Python manages memory automatically, mostly using reference counting.

But it struggles with circular references.

Example:

  • Object A references Object B
  • Object B references Object A
  • Even after deleting both, memory might not be freed

This is where Python’s garbage collector kicks in to detect and clean cycles.

You can:

  • Manually trigger garbage collection
  • Use tools like weakref (covered later)

This matters when:

  • You’re working with complex object graphs
  • You’re debugging memory leaks
  • You’re building long-running services
2.garbage_collection.pyGitHub ↗
import gc
import sys

class Node:
    def __init__(self, name):
        self.name = name
        self.next = None

# Create nodes
a = Node("A")
b = Node("B")
print(f"Initial ref count for a: {sys.getrefcount(a)}")  # One from 'a' variable
print(f"Initial ref count for b: {sys.getrefcount(b)}")  # One from 'b' variable

# Create circular reference
a.next = b
b.next = a
print("\nAfter creating circular reference:")
print(f"Ref count for a: {sys.getrefcount(a)}")  # Increased because b.next points to it
print(f"Ref count for b: {sys.getrefcount(b)}")  # Increased because a.next points to it

# Delete references
print("\nDeleting references...")
del a
del b
# Note: We can't print ref counts here because we can't access a or b anymore!

# Force garbage collection
collected = gc.collect()
print(f"\nGarbage collector collected {collected} objects")

03Not Returning Dicts & Lists from Functions

Watch at 2:28

This is subtle but important.

In Python, lists and dictionaries are passed by reference, not copied.

That means:

  • If a function modifies a list or dict, the original object is modified
  • You don’t need to return it unless you want to

Example idea:

  • Pass a list into a function
  • Modify it inside the function
  • The caller sees the changes automatically

This shows you understand:

  • Python object references
  • Mutability
  • Memory behavior
3.no_return.pyGitHub ↗
from no_return_other import my_func

my_list = []

my_func(my_list, 5)
print(my_list)
no_return_other.pyGitHub ↗
def my_func(items, n):
	for i in range(n):
		items.append(n)

04Method Resolution Order (MRO)

Watch at 3:27

MRO defines the order Python looks for methods and attributes.

This becomes critical with multiple inheritance.

Python uses something called C3 Linearization, which:

  • Resolves inheritance from left to right
  • Ensures a consistent lookup order
  • Avoids ambiguity when possible

The classic issue here is the Diamond Problem, where:

  • Two parent classes inherit from the same base class
  • A child class inherits from both parents

Understanding MRO helps prevent:

  • Unexpected behavior
  • Bugs in complex class hierarchies
4.method_resolution_order.pyGitHub ↗
class A:
    def greet(self):
        print("Hello from A")

class B:
    def greet(self):
        print("Hello from B")

class C(A, B):
    pass

c = C()
c.greet()  # Output: Hello from A
print(C.__mro__)  # (<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>)

# The diamond problem

class A:
    def greet(self):
        print("Hello from A")

class B(A):
    pass

class C(A):
    def greet(self):
        print("Hello from C")

class D(B, C):
    pass

d = D()
d.greet()  # Output: Hello from C
print(D.__mro__)  # (<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>)

05Walrus Operator (:=)

Watch at 4:17

Officially called the assignment expression.

It lets you assign and use a value in the same expression.

Instead of:

  • Assigning a variable
  • Then checking it

You can do both at once.

This:

  • Reduces boilerplate
  • Makes conditionals cleaner
  • Shows strong Python fluency

It’s small, but interviewers notice it.

5.walrus_operator.pyGitHub ↗
my_dict = {...}

def my_func(my_dict):
    my_var = my_dict.get("my_var", None)
    if my_var:
        return my_var

# With walrus operator
my_dict = {...}
def my_func(my_dict):
    if my_var := my_dict.get("my_var", None):
        return my_var

06operator.attrgetter

Watch at 5:07

This is a performance-optimized alternative to lambdas for attribute access.

It’s especially useful when:

  • Sorting objects
  • Accessing nested attributes dynamically

Example use case:

  • Sorting people by person.address.city
  • Sorting by person.company.name

Why it’s powerful:

  • Supports deeply nested attributes
  • Accepts dynamic strings
  • Implemented in C, so it’s faster than lambdas

Great for:

  • Automation systems
  • Config-driven logic
  • Clean, readable sorting logic
6.operator_attrgetter.pyGitHub ↗
from operator import attrgetter

# Define nested classes
class Address:
    def __init__(self, city, state):
        self.city = city
        self.state = state
        
class Company:
    def __init__(self, city, name):
        self.city = city
        self.name = name

class Person:
    def __init__(self, name, age, address, company):
        self.name = name
        self.age = age
        self.address = address
        self.company = company

# Sample data
people = [
    Person("Alice", 30, Address("New York", "NY"), Company("Company ABC", "Philadelphia")),
    Person("Bob", 25, Address("Chicago", "IL"), Company("Company 2", "Nashville")),
    Person("Charlie", 35, Address("Los Angeles", "CA"), Company("Company 3", "Miami"))
]

# Get sort_key from frontend
# sort_key = "address.city"
# sort_key = "company.name"
sort_key = "address.city"

# Sort based on the dynamic key
sorted_people = sorted(people, key=attrgetter(sort_key))

print([person.name for person in sorted_people])  # Example Output: ['Bob', 'Charlie', 'Alice']

When people say “Python”, they usually mean CPython.

CPython:

  • Is written in C
  • Compiles Python code into bytecode
  • Executes it using the Python Virtual Machine

Key points:

  • Python code → bytecode → executed by VM
  • Huge standard library
  • Supports C extensions for performance

Downsides:

  • Slower than compiled languages
  • Has the Global Interpreter Lock (GIL)

Understanding CPython shows you:

  • Know Python internals
  • Can reason about performance

Code for this one lives with #34 Bytecode & dis Module.

08Global Interpreter Lock (GIL)

Watch at 8:34

The GIL ensures:

Only one thread executes Python bytecode at a time

Why it exists:

  • CPython uses reference counting for memory
  • GIL keeps reference updates thread-safe
  • Simplifies memory management

Consequences:

  • Multithreading does not give true parallelism
  • CPU-bound tasks don’t scale with threads
  • IO-bound tasks still benefit

Important distinction:

  • GIL affects threads
  • It does NOT affect multiprocessing

This is a core senior-level topic.

8.global_interpreter_lock.pyGitHub ↗
import threading
import time

def cpu_task():
    for _ in range(10**7):
        pass  # Simulates CPU work

threads = [threading.Thread(target=cpu_task) for _ in range(4)]

start = time.time()
for thread in threads:
    thread.start()

for thread in threads:
    thread.join()

print(f"Time taken: {time.time() - start}")

09Concurrency vs Parallelism

Watch at 9:40

These are not the same thing.

Concurrency

  • Managing multiple tasks at once
  • Tasks are interleaved
  • Often uses context switching

Parallelism

  • Tasks run at the same time
  • Requires multiple cores or processes

Python supports concurrency via:

  • Threads
  • Async programming
  • Multiprocessing (true parallelism)

Key challenges:

  • Race conditions
  • Non-deterministic bugs
  • GIL limitations

Code for this one lives with #10 Multithreading, #11 Multiprocessing and #50 asyncio.

10Multithreading

Watch at 10:50

Threads:

  • Share the same memory
  • Are lightweight
  • Are good for IO-bound work

Because of the GIL:

  • Only one thread runs Python bytecode at a time
  • Threads take turns holding the GIL

Why threading still matters:

  • While one thread waits for IO, another can run

  • Great for:

    • Network requests
    • File IO
    • APIs

Best practice:

  • Use ThreadPoolExecutor
  • Don’t manage threads manually
10.multithreading.pyGitHub ↗
import threading
import time

def download(url):
    print(f"Starting download: {url}")
    time.sleep(2)  # Simulate download time
    print(f"Finished download: {url}")

urls = ["url1", "url2", "url3"]

threads = []
for url in urls:
    thread = threading.Thread(target=download, args=(url,))
    threads.append(thread)
    thread.start()

# Wait for all threads to finish
for thread in threads:
    thread.join()

print("All downloads completed")

from concurrent.futures import ThreadPoolExecutor
import time

def download(url):
    print(f"Downloading: {url}")
    time.sleep(2)
    print(f"Finished: {url}")

urls = ["url1", "url2", "url3"]

with ThreadPoolExecutor() as executor:
    executor.map(download, urls)

11Multiprocessing

Watch at 12:35

Multiprocessing is how you get true parallelism in Python.

Instead of threads, you create separate processes, and each process has:

  • Its own Python interpreter
  • Its own memory space
  • Its own GIL

This allows Python to:

  • Use multiple CPU cores
  • Run tasks truly at the same time

Best for:

  • CPU-bound tasks
  • Heavy computation
  • Image processing, ML workloads, numerical work

Trade-offs:

  • Higher overhead than threads
  • More complex data sharing
  • Slower startup time
11.multiprocessing.pyGitHub ↗
from multiprocessing import Process
import os

def worker(name):
    print(f"Process {name} is running in process ID: {os.getpid()}")

if __name__ == "__main__":
    process1 = Process(target=worker, args=("A",))
    process2 = Process(target=worker, args=("B",))

    process1.start()
    process2.start()

    process1.join()
    process2.join()

    print("Both processes finished")

12Race Conditions (Multiprocessing & Multithreading)

Watch at 13:49

A race condition happens when:

  • Multiple threads or processes
  • Access shared data
  • At the same time
  • And the final result depends on execution order

Example idea:

  • Two workers increment the same variable
  • Final value changes every run

This leads to:

  • Non-deterministic behavior
  • Extremely hard-to-debug bugs

How to fix it:

  • Locks
  • Semaphores
  • Synchronization primitives

Race conditions are one of the most common causes of production bugs.

12.multiprocessing_race_conditions.pyGitHub ↗
import multiprocessing

def increment(n):
    global counter
    for _ in range(100000):
        n.value += 1

if __name__ == '__main__':
    number = multiprocessing.Value('i', 0)
    p1 = multiprocessing.Process(target=increment, args=(number,))
    p2 = multiprocessing.Process(target=increment, args=(number,))
    
    p1.start()
    p2.start()
    p1.join()
    p2.join()
    
    print(number.value)
12.multiprocessing_locks.pyGitHub ↗
import multiprocessing

def increment(n, lock):
    for _ in range(100000):
        with lock:
            n.value += 1

if __name__ == '__main__':
    number = multiprocessing.Value('i', 0)

    lock = multiprocessing.Lock()
    
    p1 = multiprocessing.Process(target=increment, args=(number, lock))
    p2 = multiprocessing.Process(target=increment, args=(number, lock))
    
    p1.start()
    p2.start()
    p1.join()
    p2.join()
    
    print(number.value)

13Shared Memory in Multiprocessing

Watch at 14:52

By default, processes do not share memory.

But sometimes you need them to.

Python provides:

  • multiprocessing.Value
  • multiprocessing.Array

These allow:

  • Shared state across processes
  • Safe updates using locks

Important detail:

  • Shared memory must be explicit
  • Always use synchronization to avoid corruption

This shows you understand process isolation and performance trade-offs.

13.shared_memory_in_multiprocessing.pyGitHub ↗
from multiprocessing import Process, Value

def increment(shared_value):
    with shared_value.get_lock():  # Synchronize access
        shared_value.value += 1

if __name__ == "__main__":
    shared_value = Value('i', 0)  # Shared integer initialized to 0
    processes = [Process(target=increment, args=(shared_value,)) for _ in range(5)]

    for p in processes:
        p.start()
    for p in processes:
        p.join()

    print(shared_value.value)  # Output: 5

14collections Module

Watch at 15:24

The collections module provides specialized data structures optimized for common patterns.

Some highlights:

  • defaultdict
  • OrderedDict
  • Counter
  • deque

Why this matters:

  • Cleaner code
  • Faster execution
  • More expressive intent

Example idea:

  • defaultdict(int) avoids manual key checks
  • Perfect for counting problems and LeetCode-style tasks

Using collections shows:

  • Strong standard library knowledge
  • Practical Python experience
14.collections.pyGitHub ↗
from collections import defaultdict

# Create a defaultdict with a default value of 0
default_dict = defaultdict(int)

default_dict['a'] += 1
default_dict['b'] += 5

print(default_dict)

Encapsulation is about hiding internal state and exposing controlled access.

In Python:

  • Achieved via naming conventions
  • Double underscore (__) triggers name mangling

Example idea:

  • __balance cannot be accessed directly
  • Must use a getter method

This protects:

  • Internal invariants
  • Object integrity
  • API stability

Python doesn’t enforce privacy strictly, but convention matters.

15.encapsulation.pyGitHub ↗
class BankAccount:
    def __init__(self, owner, balance):
        self.owner = owner
        self.__balance = balance  # Private attribute

    def deposit(self, amount):
        if amount > 0:
            self.__balance += amount

    def withdraw(self, amount):
        if 0 < amount <= self.__balance:
            self.__balance -= amount

    def get_balance(self):
        return self.__balance  # Controlled access

# Usage
account = BankAccount("Alice", 1000)
print(account.get_balance())  # Output: 1000

Abstraction hides complexity and exposes only what’s necessary.

In Python, this is often done using:

  • Abstract Base Classes (ABCs)
  • The abc module

ABCs:

  • Define required methods
  • Enforce contracts
  • Prevent incomplete implementations

If a class subclasses an ABC:

  • It must implement all abstract methods
  • Or it cannot be instantiated

This is common in frameworks and large systems.

16.abstraction.pyGitHub ↗
from abc import ABC, abstractmethod

class Shape(ABC):
    """
    Abstract base class.
    Defines a strict contract for all shapes.
    """

    @abstractmethod
    def area(self) -> float:
        """Return the area of the shape."""
        pass

    @abstractmethod
    def perimeter(self) -> float:
        """Return the perimeter of the shape."""
        pass

class Rectangle(Shape):
    """
    Concrete implementation of Shape.
    """

    def __init__(self, width: float, height: float):
        self.width = width
        self.height = height

    def area(self) -> float:
        return self.width * self.height

    def perimeter(self) -> float:
        return 2 * (self.width + self.height)

shape = Rectangle(5, 10)
print(shape.area())       
print(shape.perimeter())

17Abstract Base Classes (abc)

Watch at 18:21

ABCs are Python’s way of enforcing interfaces.

Key points:

  • Use ABC and @abstractmethod
  • Child classes must implement required methods
  • Prevents silent bugs

When to use:

  • You never instantiate the parent
  • You want strict contracts
  • You want clean architecture

This is heavily used in Django, FastAPI internals, and libraries.

17.abstract_classes.pyGitHub ↗
from abc import ABC, abstractmethod

class Shape(ABC):
    @abstractmethod
    def area(self):
        pass

    @abstractmethod
    def perimeter(self):
        pass

    def concrete(self):
        return "Subscribe"

# Cannot instantiate an abstract class
# shape = Shape()  # Raises TypeError

class Rectangle(Shape):
    def __init__(self, width, height):
        self.width = width
        self.height = height

    def area(self):
        return self.width * self.height

    def perimeter(self):
        return 2 * (self.width + self.height)

# Create an instance of the subclass
rect = Rectangle(4, 5)
print(rect.area())       # Output: 20
print(rect.perimeter())  # Output: 18
print(rect.concrete())

Inheritance allows:

  • Reuse of logic
  • Shared behavior
  • Hierarchical design

Important design rule:

  • If the parent is never instantiated → use an ABC
  • If the parent is concrete → normal inheritance is fine

You can enforce behavior using:

  • NotImplementedError
  • But ABCs are cleaner and clearer

Bad inheritance causes:

  • Tight coupling
  • Fragile code
  • Maintenance nightmares
18.inheritance.pyGitHub ↗
class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        raise NotImplementedError("Subclasses must implement this method")

class Dog(Animal):
    def speak(self):
        return f"{self.name} says Woof!"

class Cat(Animal):
    def speak(self):
        return f"{self.name} says Meow!"

# Usage
dog = Dog("Buddy")
cat = Cat("Kitty")
print(dog.speak())  # Output: Buddy says Woof!
print(cat.speak())  # Output: Kitty says Meow!

Polymorphism means:

The same interface, different behavior

In Python:

  • Achieved through method overriding
  • Duck typing
  • Shared interfaces

Example idea:

  • Shape.area() behaves differently for Circle vs Rectangle

Polymorphism enables:

  • Extensibility
  • Cleaner APIs
  • Fewer conditionals

This is one of the reasons Python scales well in large codebases.

19.polymorphism.pyGitHub ↗
class Shape:
    def area(self):
        raise NotImplementedError("Subclasses must implement this method")

class Rectangle(Shape):
    def __init__(self, width, height):
        self.width = width
        self.height = height

    def area(self):
        return self.width * self.height

class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius

    def area(self):
        return 3.14 * self.radius * self.radius

# Function demonstrating polymorphism
def print_area(shape):
    print(f"The area is {shape.area()}")

# Usage
rectangle = Rectangle(5, 10)
circle = Circle(7)

print_area(rectangle)  # Output: The area is 50
print_area(circle)     # Output: The area is 153.86

20Python Data Model (Magic / Dunder Methods)

Watch at 21:21

The data model defines how objects interact with Python itself.

These are the __dunder__ methods.

Examples:

  • __init__
  • __len__
  • __repr__
  • __enter__ / __exit__

Understanding the data model lets you:

  • Make custom objects behave like built-ins
  • Improve readability
  • Build powerful abstractions

This is core senior-level knowledge.

Code for this one lives with #42 Optimizing Memory with __slots__ and #51 Callable Objects (__call__).

An iterator is any object that implements:

  • __iter__()
  • __next__()

Behavior:

  • Returns next value
  • Raises StopIteration when done

Iterators:

  • Are stateful
  • Are consumed once
  • Are everywhere in Python

Understanding iterators helps with:

  • Performance
  • Lazy evaluation
  • Custom data pipelines
21.iterators.pyGitHub ↗
class MyIterator:
    def __init__(self, start, end):
        self.current = start
        self.end = end

    def __iter__(self):
        return self  # An iterator must return itself.

    def __next__(self):
        if self.current >= self.end:
            raise StopIteration  # Signal the end of iteration.
        value = self.current
        self.current += 1
        return value

# Using the iterator
iterator = MyIterator(1, 5)
for num in iterator:
    print(num)  # Output: 1, 2, 3, 4

Generators are a special way to create iterators.

Defined using:

  • yield

Key properties:

  • Lazy evaluation
  • Minimal memory usage
  • Pause and resume execution

Important detail:

  • Calling a generator function does NOT execute it
  • Execution starts when next() is called

Generators are essential for:

  • Streaming data
  • Large datasets
  • Memory-sensitive applications
22.generators.pyGitHub ↗
def my_generator():
    yield 1
    yield 2
    yield 3

# This defines a generator function but doesn't execute it
gen = my_generator()

print(next(gen))  # Output: 1
print(next(gen))  # Output: 2
print(next(gen))  # Output: 3
print(next(gen))  # Raises StopIteration

def generator_with_send():
    value = yield "Start"
    yield f"Received: {value}"

gen = generator_with_send()
print(next(gen))       # Output: Start
print(gen.send("Data"))  # Output: Received: Data
print(gen.throw(ValueError))

23@staticmethod and @classmethod

Watch at 24:14

These change how methods behave.

Class methods:

  • Receive the class (cls)
  • Can be called on class or instance
  • Used for factory methods or class-level logic

Static methods:

  • Receive nothing automatically
  • Behave like namespaced functions
  • Used when logic belongs to class conceptually

Choosing the right one improves:

  • Readability
  • Design clarity
  • API correctness
23.staticmethods_and_classmethods.pyGitHub ↗
class BankAccount:
    interest_rate = 0.03  # Default interest rate for all accounts

    def __init__(self, account_type, balance):
        self.account_type = account_type
        self.balance = balance

    @staticmethod
    def is_valid_transaction(amount):
        """Check if the transaction amount is valid."""
        return amount > 0

    @classmethod
    def create_savings_account(cls, initial_deposit):
        """Factory method to create a savings account."""
        if not cls.is_valid_transaction(initial_deposit):
            raise ValueError("Initial deposit must be positive.")
        return cls("Savings", initial_deposit)

    @classmethod
    def create_business_account(cls, initial_deposit):
        """Factory method to create a business account with a higher interest rate."""
        if not cls.is_valid_transaction(initial_deposit):
            raise ValueError("Initial deposit must be positive.")
        account = cls("Business", initial_deposit)
        account.interest_rate = 0.05  # Business accounts have a higher interest rate
        return account

# Example usage
try:
    # Utility function
    print(BankAccount.is_valid_transaction(-50))  # Output: False
    print(BankAccount.is_valid_transaction(100))  # Output: True
    b = BankAccount("commercial", 100)
    print(b.is_valid_transaction(100))

    # Creating accounts using class methods
    savings = BankAccount.create_savings_account(1000)
    business = BankAccount.create_business_account(5000)

    print(f"Savings Account - Balance: ${savings.balance}, Interest Rate: {savings.interest_rate}")
    # Output: Savings Account - Balance: $1000, Interest Rate: 0.03

    print(f"Business Account - Balance: ${business.balance}, Interest Rate: {business.interest_rate}")
    # Output: Business Account - Balance: $5000, Interest Rate: 0.05

except ValueError as e:
    print(e)

24Dependency Injection

Watch at 25:14

Dependency Injection means:

  • Dependencies are provided externally
  • Not created inside the class

Why it matters:

  • Looser coupling
  • Easier testing
  • More flexible design

Common form:

  • Constructor injection

This allows:

  • Swapping implementations
  • Mocking in tests
  • Cleaner architecture

Used heavily in:

  • Backend systems
  • Large services
  • Testable codebases
24.without_dependency_injection.pyGitHub ↗
class PayPalService:
    def process_payment(self, amount):
        print(f"Processing payment of ${amount} through PayPal.")

class PaymentProcessor:
    def __init__(self):
        self.payment_service = PayPalService()

    def pay(self, amount):
        self.payment_service.process_payment(amount)

processor = PaymentProcessor()
processor.pay(100)
24.with_dependency_injection.pyGitHub ↗
class PayPalService:
    def process_payment(self, amount):
        print(f"Processing payment of ${amount} through PayPal.")

class PaymentProcessor:
    def __init__(self, payment_service):
        self.payment_service = payment_service

    def pay(self, amount):
        self.payment_service.process_payment(amount)

# Inject dependency
payment_service = PayPalService()
processor = PaymentProcessor(payment_service)
processor.pay(100)

25Parameterized Testing

Watch at 26:38

Parameterized testing lets you:

  • Run the same test
  • With multiple inputs
  • In a clean, readable way

Using pytest:

  • Pass lists of inputs
  • Avoid duplicated tests
  • Improve coverage

This is essential for:

  • Backend development
  • API testing
  • Data validation

Shows:

  • Testing maturity
  • Professional engineering habits
25..pyGitHub ↗
import pytest

@pytest.mark.parametrize("user_id,expected_name", [
    (1, "Alice"),
    (2, "Bob"),
    (3, "Charlie"),
    (3, "Charlie"),
    (3, "Charlie"),
    (8, "Gary"),
])
def test_get_user_details(user_id, expected_name):
    # Simulating an API function
    def fetch_user_details(user_id):
        users = {1: "Alice", 2: "Bob", 3: "Charlie", 8: "Gary"}
        return {"id": user_id, "name": users.get(user_id, "Unknown")}
    
    response = fetch_user_details(user_id)
    assert response["name"] == expected_name

26Fixtures (Testing Setup & Teardown)

Watch at 26:59

Fixtures are how you prepare and clean up test environments in pytest.

Key idea:

  • A fixture runs before a test
  • Yields a resource
  • Cleanup happens after the test finishes

This is extremely useful for:

  • Database connections
  • Temporary files
  • External services

Instead of setting up and tearing down for every test, you:

  • Do it once
  • Reuse it safely
  • Keep tests clean and readable

This is backend-engineer-level testing knowledge.

26.pytest_fixtures.pyGitHub ↗
import pytest
import sqlite3

@pytest.fixture
def temp_db():
    # Setup: Create an in-memory SQLite database
    conn = sqlite3.connect(":memory:")
    conn.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)")
    conn.execute("INSERT INTO users (name) VALUES ('Alice')")
    yield conn
    # Teardown: Close the connection
    conn.close()

def test_query_user(temp_db):
    cursor = temp_db.cursor()
    cursor.execute("SELECT name FROM users WHERE id=1")
    user = cursor.fetchone()
    assert user[0] == "Alice"

27Serialization & Deserialization

Watch at 27:50

Serialization is converting objects into a format that can be:

  • Stored on disk
  • Sent over the network
  • Cached
  • Reconstructed later

In Python, this is often done with:

  • pickle
  • JSON (for interoperability)

Common use cases:

  • Saving ML models
  • Storing configs
  • Caching expensive results

Deserialization restores the object back to its original state.

Important note:

  • Pickle is powerful but not secure for untrusted data
27.serialization_deserialization.pyGitHub ↗
import pickle

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def greet(self):
        return f"Hello, my name is {self.name} and I am {self.age} years old."

# Create an instance of the class
person = Person("Alice", 30)

# Serialize (pickle) the instance to a file
with open("person.pkl", "wb") as file:
    pickle.dump(person, file)

# Deserialize (unpickle) the instance from the file
with open("person.pkl", "rb") as file:
    loaded_person = pickle.load(file)

print(loaded_person.greet())  # Output: Hello, my name is Alice and I am 30 years old.

28__getstate__ and __setstate__

Watch at 28:39

These methods customize how objects are pickled.

Why they exist:

  • Some objects cannot be serialized
  • File handles, sockets, threads, etc.

__getstate__:

  • Called during serialization
  • Remove or transform unserializable fields

__setstate__:

  • Called during deserialization
  • Restore missing state

This shows deep understanding of:

  • Object lifecycle
  • Serialization internals
28.__getstate___and__setstate__.pyGitHub ↗
import pickle

class FileHandler:
    def __init__(self, filename):
        self.filename = filename
        self.file = open(filename, "w")

    def write(self, data):
        self.file.write(data)

    def __getstate__(self):
        state = self.__dict__.copy()
        # Remove the non-pickleable file object
        del state["file"]
        return state

    def __setstate__(self, state):
        self.__dict__.update(state)
        # Restore the file object
        self.file = open(self.filename, "w")

# Create an instance
handler = FileHandler("example.txt")
handler.write("Hello, World!")

# Serialize and deserialize the instance
with open("handler.pkl", "wb") as file:
    pickle.dump(handler, file)

with open("handler.pkl", "rb") as file:
    loaded_handler = pickle.load(file)

loaded_handler.write("Another line!")

heapq implements a priority queue using a binary heap.

Key properties:

  • Min-heap by default
  • Fast insert: O(log n)
  • Fast pop: O(log n)
  • Fast access to smallest element: O(1)

Why not use a list?

  • Lists require O(n) inserts/removals
  • Heaps scale much better

Common use cases:

  • Task schedulers
  • Shortest-path algorithms
  • Anytime “highest priority first” matters
29.heapq.pyGitHub ↗
import heapq

class TaskScheduler:
    def __init__(self):
        # Priority queue (min-heap)
        self.task_queue = []

    def add_task(self, priority, task_name):
        """
        Add a task to the priority queue.
        Priority: Lower values indicate higher priority.
        """
        heapq.heappush(self.task_queue, (priority, task_name))
        print(f"Task '{task_name}' added with priority {priority}")

    def execute_task(self):
        """
        Execute the highest-priority task.
        """
        if self.task_queue:
            priority, task_name = heapq.heappop(self.task_queue)
            print(f"Executing task '{task_name}' with priority {priority}")
        else:
            print("No tasks to execute.")

    def peek_next_task(self):
        """
        Peek at the highest-priority task without removing it.
        """
        if self.task_queue:
            priority, task_name = self.task_queue[0]
            print(f"Next task to execute: '{task_name}' with priority {priority}")
        else:
            print("No tasks in the queue.")

# Example Usage
if __name__ == "__main__":
    scheduler = TaskScheduler()

    # Add tasks
    scheduler.add_task(3, "Write report")
    scheduler.add_task(1, "Fix critical bug")
    scheduler.add_task(2, "Attend team meeting")

    # Peek at the next task
    scheduler.peek_next_task()

    # Execute tasks in order of priority
    scheduler.execute_task()
    scheduler.execute_task()
    scheduler.execute_task()

    # Try to execute with no tasks
    scheduler.execute_task()

30Higher-Order Functions

Watch at 31:25

A higher-order function:

  • Takes a function as input
  • Or returns a function

This enables:

  • Reusable logic
  • Clean abstractions
  • Functional-style programming

You see this a lot in:

  • Validators
  • Callbacks
  • Middleware
  • Decorators

Frameworks like Django rely heavily on this pattern.

30.higher_order_functions.pyGitHub ↗
def validate(data, *validators):
    for validator in validators:
        if not validator(data):
            return False
    return True

# Validators
is_non_empty = lambda x: bool(x)
is_alpha = lambda x: x.isalpha()

# Validate input
input_data = "Python"
is_valid = validate(input_data, is_non_empty, is_alpha)
print(is_valid)  # Output: True

filter is a built-in higher-order function.

It:

  • Takes a predicate function
  • Returns only items where predicate is True

This replaces:

  • Verbose loops
  • Manual condition checks

Example idea:

  • Filtering even numbers
  • Validating datasets

Using filter shows:

  • Standard library fluency
  • Clean Python style
31.filter.pyGitHub ↗
numbers = [1, 2, 3, 4]
evens = filter(lambda x: x % 2 == 0, numbers)
print(list(evens))  # Output: [2, 4]

32Advanced List Comprehensions

Watch at 32:59

List comprehensions can do much more than simple loops.

You can:

  • Nest loops
  • Add conditionals
  • Call functions
  • Transform data inline

This allows:

  • Very expressive one-liners
  • Cleaner and faster code
  • Reduced boilerplate

Senior engineers know when to use this—and when not to overdo it.

32.advanced_list_comprehension.pyGitHub ↗
# matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# flattened = [num for row in matrix for num in row]
# print(flattened)  # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]

# numbers = [1, 2, 3, 4, 5]
# labels = ["even" if n % 2 == 0 else "odd" for n in numbers]
# print(labels)  # Output: ['odd', 'even', 'odd', 'even', 'odd']

def celsius_to_fahrenheit(c):
    return (c * 9/5) + 32

temperatures_c = [0, 20, 30, 40]
temperatures_f = [celsius_to_fahrenheit(t) for t in temperatures_c]
print(temperatures_f)  # Output: [32.0, 68.0, 86.0, 104.0]

bytes represents binary data.

Key properties:

  • Immutable
  • Values from 0–255
  • Common in low-level work

Used for:

  • Files
  • Network packets
  • Images
  • Audio
  • Protocols

Understanding bytes is critical when dealing with:

  • File I/O
  • APIs
  • Performance-sensitive systems
33.bytes.pyGitHub ↗
# Write binary data to a file
with open("example.bin", "wb") as file:
    file.write(b"Binary data")

# Read binary data from a file
with open("example.bin", "rb") as file:
    data = file.read()
    print(data)  # Output: b'Binary data'

34Bytecode & dis Module

Watch at 34:36

Python source code is compiled into bytecode before execution.

The dis module lets you:

  • Inspect bytecode
  • See how Python executes code
  • Identify inefficiencies

This helps with:

  • Performance optimization
  • Understanding interpreter behavior
  • Learning Python internals

Senior engineers don’t use this daily—but knowing it exists matters.

34.python_bytecode.pyGitHub ↗
import dis 

def count_to_ten():
    for i in range(10):
        print(i)

dis.dis(count_to_ten)

memoryview lets you:

  • Work with slices of data
  • Without copying it

This is critical for:

  • Large binary files
  • Network buffers
  • Memory efficiency

Normal slicing creates copies. memoryview creates views.

Using this can massively reduce memory usage in data-heavy systems.

35.memoryview.pyGitHub ↗
import sys
data = bytearray(b"A" * 10**6)
mv = memoryview(data)

# Slicing without copying
mv_slice = mv[100:100000]  # View of the data, no copy made

# Slicing with bytes
bytes_slice = data[100:100000]  # Creates a new copy

# Compare sizes
print(f"Size of memoryview slice: {sys.getsizeof(mv_slice):,} bytes")
print(f"Size of bytes slice: {sys.getsizeof(bytes_slice):,} bytes")

Metaclasses define how classes themselves are created.

By default:

  • Every class uses type as its metaclass

With custom metaclasses you can:

  • Modify class creation
  • Inject attributes
  • Enforce rules
  • Generate behavior dynamically

This is advanced but very powerful.

In the wild: Django models

Django models use a custom metaclass.

What happens:

  • You define a class
  • Metaclass intercepts creation
  • Fields are collected
  • Database mappings are generated
  • Managers are attached

This explains:

  • Why Django models feel “magic”
  • How frameworks automate behavior

Understanding this separates advanced users from power users.

36.metaclasses.pyGitHub ↗
class Dog:
    pass
# python secretly does this 
# () is a tuple of base classes (e.g., if the class inherits from something).
# {} is the dictionary containing the class's methods and attributes.
Dog = type("Dog", (), {})

# from django.db import models

# class MyModel(models.Model):
#     name = models.CharField(max_length=100)
#     age = models.IntegerField()

# class ModelMeta(type):
#     def __new__(cls, name, bases, dct):
#         # Extract fields (attributes that are instances of Field)
#         fields = {key: value for key, value in dct.items() if isinstance(value, Field)}
        
#         # Add the _meta attribute to store metadata about the model
#         dct["_meta"] = {"fields": fields}
        
#         # Add a custom manager
#         dct["objects"] = DatabaseManager()
        
#         return super().__new__(cls, name, bases, dct)

37Nesting & Combining Context Managers

Watch at 37:58

You can nest context managers using:

with open(a), open(b):
    ...

When context managers are dynamic:

  • Use contextlib.ExitStack

This allows:

  • Programmatic management
  • Dynamic resource handling
  • Clean teardown logic

Very useful for:

  • Files
  • DB connections
  • Resource pools
37.nesting_and_combining_context_managers.pyGitHub ↗
# with open('file1.txt', 'r') as file1, open('file2.txt', 'w') as file2:
#     data = file1.read()
#     file2.write(data)

from contextlib import ExitStack

files = ['file1.txt', 'file2.txt']

with ExitStack() as stack:
    file_objects = [stack.enter_context(open(file, 'w')) for file in files]
    for file_obj in file_objects:
        file_obj.write("Hello, World!")

38Custom Context Managers

Watch at 38:45

You can create context managers using:

  • Classes with __enter__ / __exit__
  • Or generators with @contextmanager

Generator-based context managers:

  • Are simpler
  • Are more readable
  • Are very common in production code

Especially useful for:

  • Database transactions
  • Temporary states
  • Resource handling
38.custom_context_managers.pyGitHub ↗
import sqlite3
from contextlib import contextmanager

@contextmanager
def database_connection(db_name):
    """Context manager for managing a database connection."""
    conn = sqlite3.connect(db_name)
    try:
        print("Database connection opened")
        yield conn
    finally:
        conn.close()
        print("Database connection closed")

# Usage
with database_connection("example.db") as conn:
    cursor = conn.cursor()
    cursor.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER, name TEXT)")
    cursor.execute("INSERT INTO users (id, name) VALUES (?, ?)", (1, "Alice"))
    conn.commit()
    print("Data inserted successfully")

Weak references do not increase reference count.

This allows:

  • Garbage collection of objects
  • Even if they are referenced weakly

Useful for:

  • Caching
  • Metadata storage
  • Avoiding memory leaks

Downside:

  • Object may disappear unexpectedly
  • Must ensure strong references exist when needed

This shows deep GC and memory knowledge.

39.weakref.pyGitHub ↗
import weakref

class Node:
    def __init__(self, name):
        self.name = name
        self.next = None

# Avoid circular references using weak references
a = Node("A")
b = Node("B")
a.next = weakref.ref(b)
b.next = weakref.ref(a)

# Access weak references
print(a.next().name)  # Output: "B"
print(b.next().name)  # Output: "A"

del removes a name, not an object.

What actually happens:

  • del x removes the name x from the current namespace
  • The object's reference count drops by one
  • The object is only freed when no references remain

So del does not directly free memory. If anything else still points at the object (a list, a cache, a closure, a circular reference), it stays alive.

del also works on:

  • List items and slices: del items[0], del items[::2]
  • Dict keys: del config["debug"]
  • Attributes: del obj.attr

Don't confuse it with __del__, the finalizer that runs when an object is about to be destroyed. You can't rely on when that happens, so prefer context managers for cleanup.

Code for this one lives with #2 Garbage Collection & Circular References and #39 weakref.

41WeakKeyDictionary & WeakValueDictionary

Watch at 40:06

These are specialized dictionaries from the weakref module.

What makes them different:

  • Keys or values are held as weak references
  • Objects can still be garbage collected
  • Entries disappear automatically when objects are destroyed

Use cases:

  • Attaching metadata to objects
  • Caches that should not keep objects alive
  • Observer-style patterns

This prevents subtle memory leaks in long-running systems.

41.weak_key_dictionary.pyGitHub ↗
from weakref import WeakKeyDictionary

class Widget:
    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return f"Widget({self.name})"

# Create a WeakKeyDictionary for storing metadata
widget_metadata = WeakKeyDictionary()

# Create some widgets
widget1 = Widget("Button1")
widget2 = Widget("Button2")

# Attach metadata
widget_metadata[widget1] = {"color": "blue", "size": "small"}
widget_metadata[widget2] = {"color": "red", "size": "large"}

print(widget_metadata)  # Output: {Widget(Button1): {...}, Widget(Button2): {...}}

# Remove a strong reference
del widget1

# The metadata for widget1 is automatically cleaned up
print([key for key in widget_metadata.keys()])  # Output: {Widget(Button2): {...}}

42Optimizing Memory with __slots__

Watch at 41:17

By default, Python stores instance attributes in a dynamic __dict__.

__slots__:

  • Explicitly declares allowed attributes
  • Prevents creation of __dict__
  • Reduces memory usage per instance

Best for:

  • Classes with many instances
  • Performance-critical systems
  • Data-heavy applications

Tradeoff:

  • Less flexibility
  • Slightly more rigid design
_slots.pyGitHub ↗
from memory_profiler import profile

class UserProfile:
    def __init__(self, username, email):
        self.username = username
        self.email = email

class SlotsUserProfile:
    __slots__ = ['username', 'email']

    def __init__(self, username, email):
        self.username = username
        self.email = email

@profile
def profile_creation(my_class): 
    profiles = [my_class(f"user{i}", f"user{i}@example.com") for i in range(1000000)]
    return profiles

profile_creation(UserProfile)
profile_creation(SlotsUserProfile)

43memory_profiler

Watch at 42:14

A tool for tracking memory usage line-by-line.

Why it matters:

  • Identifies memory leaks
  • Shows which functions consume memory
  • Critical for optimizing large systems

Usage pattern:

  • Decorate functions
  • Run program
  • Analyze memory growth

This is production-level debugging, not beginner tooling.

43.memory_profiler.pyGitHub ↗
from memory_profiler import profile

@profile
def my_function():
    a = [i for i in range(100000)]  # Consumes memory
    b = [i * 2 for i in a]         # More memory consumption
    return b

if __name__ == "__main__":
    my_function()

44sys.getsizeof()

Watch at 42:59

Returns the shallow size of an object in bytes.

Important detail:

  • Does NOT include referenced objects
  • Only the object itself

Useful for:

  • Comparing object sizes
  • Estimating memory impact
  • Understanding storage costs

Often used alongside memory profilers for deeper analysis.

44.getsizeof.pyGitHub ↗
import sys

class MyClass:
    my_var = "foo"

my_list = [MyClass()] * 10000000
print(len(my_list))
print(sys.getsizeof(my_list))

45Advanced Decorators

Watch at 43:33

Decorators can do much more than logging.

Advanced uses include:

  • Attaching state to functions
  • Counting calls
  • Caching results
  • Authentication and authorization
  • Stacking multiple behaviors

They can:

  • Wrap functions
  • Wrap classes
  • Accept arguments themselves

This demonstrates strong functional programming skills.

45.advanced_decorators.pyGitHub ↗
# def call_counter(func):
#     def wrapper(*args, **kwargs):
#         wrapper.calls += 1
#         print(f"Call {wrapper.calls} to {func.__name__}")
#         return func(*args, **kwargs)
#     wrapper.calls = 0  # Add a counter attribute to the wrapper
#     return wrapper

# @call_counter
# def greet(name):
#     print(f"Hello, {name}!")

# greet("Alice")  # Call 1 to greet
# greet("Bob")    # Call 2 to greet

# # So you can use decorators for caching
# def cache(func):
#     stored_results = {}
#     def wrapper(*args):
#         if args not in stored_results:
#             stored_results[args] = func(*args)
#         return stored_results[args]
#     return wrapper

# @cache
# def fib(n):
#     if n < 2:
#         return n
#     return fib(n - 1) + fib(n - 2)

# print(fib(10))  # Output: 55

# def authenticate(func):
#     def wrapper(*args, **kwargs):
#         print("Authenticating...")
#         return func(*args, **kwargs)
#     return wrapper

# def log(func):
#     def wrapper(*args, **kwargs):
#         print(f"Logging call to {func.__name__}")
#         return func(*args, **kwargs)
#     return wrapper

# @authenticate
# @log
# def access_data():
#     print("Accessing sensitive data")

# access_data()
# Output:
# Authenticating...
# Logging call to access_data
# Accessing sensitive data

# def add_method(cls):
#     def new_method(self):
#         return "I am a new method!"
#     cls.new_method = new_method
#     return cls

# @add_method
# class MyClass:
#     pass

# obj = MyClass()
# print(obj.new_method())  # Output: I am a new method!

import time

def timer(unit="seconds"):
    def decorator(func):
        def wrapper(*args, **kwargs):
            start = time.time()
            result = func(*args, **kwargs)
            end = time.time()
            duration = end - start
            if unit == "milliseconds":
                duration *= 1000
            print(f"{func.__name__} took {duration:.2f} {unit}")
            return result
        return wrapper
    return decorator

@timer(unit="milliseconds")
def slow_function():
    time.sleep(1)

slow_function()  # Output: slow_function took 1000.00 milliseconds

Dataclasses remove boilerplate from data-heavy classes.

They automatically generate:

  • __init__
  • __repr__
  • __eq__
  • __hash__ (optional)

Perfect for:

  • DTOs
  • Config objects
  • Domain models

They make code:

  • Cleaner
  • More readable
  • Easier to maintain
46.dataclasses.pyGitHub ↗
from dataclasses import dataclass

# @dataclass
# class Point:
#     x: int
#     y: int

# point = Point(1, 2)
# print(point)        # Output: Point(x=1, y=2)
# print(point.x)      # Output: 1
# print(point.y)      # Output: 2

# class Point:
#     def __init__(self, x: int, y: int):
#         self.x = x
#         self.y = y
    
#     def __repr__(self):
#         return f"Point(x={self.x}, y={self.y})"
    
#     def __eq__(self, other):
#         if not isinstance(other, Point):
#             return NotImplemented
#         return (self.x, self.y) == (other.x, other.y)
    
#     def __hash__(self):
#         return hash((self.x, self.y))

# point = Point(1, 2)
# print(point)        # Output: Point(x=1, y=2)
# print(point.x)      # Output: 1
# print(point.y)      # Output: 2

# # The dataclass also generates these behaviors:
# point1 = Point(1, 2)
# point2 = Point(1, 2)
# print(point1 == point2)  # True - equality comparison works
# print(hash(point1))      # Hash value based on x and y

# @dataclass
# class Person:
#     name: str
#     age: int = 30
#     city: str = "Unknown"

# person = Person(name="Alice")
# print(person)  # Output: Person(name='Alice', age=30, city='Unknown')

@dataclass(frozen=True)
class Point:
    x: int
    y: int

point = Point(1, 2)
point.x = 3  # Raises FrozenInstanceError

47Metaprogramming

Watch at 46:49

Metaprogramming is code that modifies or generates code.

In Python this includes:

  • Decorators
  • Metaclasses
  • Dynamic attribute creation
  • Runtime inspection

Used heavily in:

  • Frameworks
  • ORMs
  • Dependency injection systems

Powerful, but must be used carefully to avoid unreadable code.

Code for this one lives with #36 Metaclasses and #45 Advanced Decorators.

The functools module provides function utilities.

Key tools:

  • wraps → preserves metadata in decorators
  • lru_cache → memoization
  • reduce → cumulative operations

These help:

  • Avoid reimplementing patterns
  • Write cleaner abstractions
  • Improve performance

Interviewers love seeing standard library mastery.

48.functools.pyGitHub ↗
from functools import wraps

def my_decorator(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        return func(*args, **kwargs)
    return wrapper

# from functools import lru_cache

# @lru_cache(maxsize=3)  # Cache up to 3 results
# def expensive_computation(x):
#     print(f"Computing {x}")
#     return x * x

# print(expensive_computation(2))  # Output: Computing 2, then 4
# print(expensive_computation(2))  # Output: 4 (from cache)

from functools import reduce

numbers = [1, 2, 3, 4]
result = reduce(lambda x, y: x * y, numbers)  # Multiplies all numbers
print(result)  # Output: 24

49Advanced Dataclass Features

Watch at 49:00

Dataclasses support advanced customization.

Examples:

  • __post_init__ for validation
  • Inheritance between dataclasses
  • field() for fine-grained control
  • frozen=True for immutability

This allows:

  • Safer models
  • Controlled initialization
  • Clean validation logic

Very common in modern Python codebases.

49.advanced_dataclass_features.pyGitHub ↗
from dataclasses import dataclass, field

# @dataclass
# class Person:
#     name: str
#     age: int

#     def __post_init__(self):
#         if self.age < 0:
#             raise ValueError("Age cannot be negative")

# person = Person("Alice", 30)  # Works fine
# person = Person("Bob", -5)  # Raises ValueError

# @dataclass
# class Person:
#     name: str
#     age: int

# @dataclass
# class Employee(Person):
#     employee_id: int

# emp = Employee(name="Alice", age=30, employee_id=1234)
# print(emp)  # Output: Employee(name='Alice', age=30, employee_id=1234)

# Without field (problematic):
@dataclass
class TaskWithoutField:
    description: str
    completed: bool = False  # This becomes a parameter with default value

# This allows unwanted initialization:
task1 = TaskWithoutField("Do laundry", completed=True)  # Can override default

# With field (better design):
@dataclass
class TaskWithField:
    description: str
    completed: bool = field(init=False)  # Not in __init__
    
    def __post_init__(self):
        self.completed = False

# Now this is impossible:
# task2 = TaskWithField("Do laundry", completed=True)  # Error!
# Can only create like this:
task2 = TaskWithField("Do laundry", completed=True)  # completed is always False initially

asyncio enables asynchronous, non-blocking programming.

Core concepts:

  • Coroutines (async def)
  • await
  • Event loop
  • Tasks
  • Queues

Best for:

  • Network I/O
  • APIs
  • Databases
  • Web scraping
  • Real-time systems

It provides concurrency without threads or processes and is a core skill for modern Python engineers.

50.asyncio.pyGitHub ↗
# import asyncio

# async def greet():
#     print("Hello!")
#     await asyncio.sleep(1)  # Pause for 1 second
#     print("World!")

# asyncio.run(greet())  # Output: Hello! (pause) World!

# import asyncio

# async def task_1():
#     print("Task 1: Start")
#     await asyncio.sleep(2)
#     print("Task 1: End")

# async def task_2():
#     print("Task 2: Start")
#     await asyncio.sleep(1)
#     print("Task 2: End")

# async def main():
#     # Run tasks concurrently
#     await asyncio.gather(task_1(), task_2())

# asyncio.run(main())

import asyncio

async def producer(queue):
    for i in range(5):
        await asyncio.sleep(1)
        await queue.put(f"Item {i}")
        print(f"Produced Item {i}")

async def consumer(queue):
    while True:
        item = await queue.get()
        print(f"Consumed {item}")
        queue.task_done()

async def main():
    queue = asyncio.Queue()
    await asyncio.gather(producer(queue), consumer(queue))

asyncio.run(main())

Bonus

4 more

Added to the repo after the video, so there's no timestamp for these.

51Callable Objects (__call__)

Objects can be made callable by defining a __call__ method.

What this enables:

  • Instances behave like functions
  • Objects can retain internal state
  • Configuration and behavior live together

Use cases:

  • Function-like objects with state
  • Strategy-style patterns
  • Reusable, parameterized behavior

This is cleaner than closures when state must persist across calls.

51.call_method.pyGitHub ↗
"""
In this example, the Multiplier class has a __call__ method. When you create an instance of Multiplier with a certain factor, that instance becomes callable. 
So when you use parentheses with that object, it triggers the __call__ method and applies the factor to the given value. """
class Multiplier:
    def __init__(self, factor):
        self.factor = factor

    def __call__(self, value):
        return self.factor * value

# Example usage:
doubler = Multiplier(2)
print(doubler(5))  # Output: 10

tripler = Multiplier(3)
print(tripler(5))  # Output: 15

52Multiprocessing with Queue

The multiprocessing module enables true parallelism using separate processes.

Key concepts:

  • Each process has its own memory space
  • Queue allows safe inter-process communication
  • Execution order is non-deterministic

Use cases:

  • CPU-bound workloads
  • Multi-stage processing pipelines
  • Parallel data transformation

This pattern avoids the GIL and scales across CPU cores.

52.multiprocess_with_queue.pyGitHub ↗
from multiprocessing import Process, Queue

def producer(q1):
    for x in [1, 2, 3]:
        print("Producer:", x)
        q1.put(x)
    q1.put(None)

def add_one(q1, q2):
    while True:
        x = q1.get()
        if x is None:
            q2.put(None)
            break
        y = x + 1
        print("Add one:", y)
        q2.put(y)

def multiply(q2):
    while True:
        x = q2.get()
        if x is None:
            break
        print("Multiply:", x * 5)

if __name__ == "__main__":
    q1 = Queue()
    q2 = Queue()

    p1 = Process(target=producer, args=(q1,))
    p2 = Process(target=add_one, args=(q1, q2))
    p3 = Process(target=multiply, args=(q2,))

    # What will be printed (order may vary because processes run in parallel):
    #   Producer: 1
    #   Add one: 2
    #   Multiply: 10
    #   Producer: 2
    #   Add one: 3
    #   Multiply: 15
    #   Producer: 3
    #   Add one: 4
    #   Multiply: 20
    #
    # Notes:
    # - Exact interleaving is NOT guaranteed.
    # - These 9 lines are the expected set of outputs.

    p1.start()
    p2.start()
    p3.start()

    p1.join()
    p2.join()
    p3.join()

53Typing: Protocols & Generics

Protocols and generics are tools from Python’s static typing system that allow you to write flexible APIs without losing type safety.

Common use cases:

  • Polymorphic interfaces (duck typing)
  • Flexible function/type arguments
  • Safer, clearer APIs
53.typing_protocols_and_generics.pyGitHub ↗
"""Small, commented examples: Protocols, Generics and ParamSpec.

Keep this file short and runnable. Intended as a quick demo for contributors.
"""

from typing import Protocol, runtime_checkable, TypeVar, Generic, Callable, ParamSpec

P = ParamSpec("P")
R = TypeVar("R")
T = TypeVar("T")

@runtime_checkable
class SupportsClose(Protocol):
    """Structural type: any object with a close() method fits."""
    def close(self) -> None: ...

class Socket:
    """Simple object that implements close()."""
    def __init__(self, name: str) -> None:
        self.name = name

    def close(self) -> None:
        print(f"Socket {self.name} closed")

def close_if_supported(obj: SupportsClose) -> None:
    """Call .close() on anything that matches the Protocol.

    This shows structural typing: no inheritance required.
    """
    obj.close()

class Box(Generic[T]):
    """Tiny generic container.

    Static type checkers will remember the element type.
    """
    def __init__(self, value: T) -> None:
        self._v = value

    def get(self) -> T:
        return self._v

# ParamSpec decorator: preserves caller signature for typing tools.
def make_logged(func: Callable[P, R]) -> Callable[P, R]:
    def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:  # type: ignore[misc]
        print(f"[log] {func.__name__} args={args} kwargs={kwargs}")
        res = func(*args, **kwargs)
        print(f"[log] {func.__name__} -> {res}")
        return res

    return wrapper

@make_logged
def greet(name: str, excited: bool = False) -> str:
    return "Hello, " + name + ("!!!" if excited else ".")

if __name__ == "__main__":
    # Protocol demo
    s = Socket("A")
    close_if_supported(s)

    # Generic demo
    bi = Box[int](10)
    bs = Box[str]("ok")
    print(type(bi.get()), bi.get())
    print(type(bs.get()), bs.get())

    # ParamSpec demo (runtime logs and preserved signature for mypy)
    print(greet("World"))
    print(greet("Lin", excited=True))

54Descriptors (Typed + CachedProperty)

Descriptors let you control how attributes are accessed and stored on objects. They're used to add type enforcement, lazy computation, or caching to attribute access.

Common use cases:

  • Type validation on assignment
  • Cached/computed properties (like @property but cached)
  • Logging or auditing attribute changes
  • Read-only or write-only attributes
  • Custom attribute behavior in libraries and frameworks
54.descriptors.pyGitHub ↗
"""Short descriptor examples with human-like comments.

Shows a Typed data descriptor and a simple cached property.
"""

from typing import Any

class Typed:
    """Data descriptor that enforces the value type.

    We store the value in the instance.__dict__ under the provided name.
    This raises on wrong types so callers get early feedback.
    """

    def __init__(self, name: str, expected_type: type) -> None:
        self.name = name
        self.expected_type = expected_type

    def __set_name__(self, owner, name):
        # saves the attribute name if the class assigns a different one
        self.public_name = name

    def __get__(self, instance: Any, owner: type = None) -> Any:
        # when accessed on the class, return the descriptor itself
        if instance is None:
            return self
        return instance.__dict__.get(self.name)

    def __set__(self, instance: Any, value: Any) -> None:
        # enforce the expected type
        if not isinstance(value, self.expected_type):
            raise TypeError(f"{self.name} must be {self.expected_type.__name__}")
        instance.__dict__[self.name] = value

class CachedProperty:
    """A tiny cached-property: compute once, then store on the instance.

    This is a non-data descriptor (no __set__), so assigning the cached name
    on the instance bypasses future descriptor lookups.
    """

    def __init__(self, func):
        self.func = func
        self.__doc__ = getattr(func, '__doc__')

    def __get__(self, instance, owner=None):
        if instance is None:
            return self
        value = self.func(instance)
        instance.__dict__[self.func.__name__] = value
        return value

class Point:
    # descriptors: declare at the class level
    x = Typed('x', int)
    y = Typed('y', int)

    def __init__(self, x: int, y: int) -> None:
        # these assignments go through Typed.__set__
        self.x = x
        self.y = y

    @CachedProperty
    def hypot(self):
        # small computation that we'd like to cache
        print('computing hypot')
        from math import hypot
        return hypot(self.x, self.y)

if __name__ == '__main__':
    p = Point(3, 4)
    print('x, y:', p.x, p.y)

    # wrong type assignment shows the guard
    try:
        p.x = 2.5
    except TypeError as e:
        print('caught type error:', e)

    # cached property: first access computes, second returns cached value
    print('first hypot:', p.hypot)
    print('second hypot (cached):', p.hypot)
    print('instance keys:', list(p.__dict__.keys()))

Know the concepts, but not sure how you come across in the interview?

I run mock interviews the way I ran them as a hiring engineer, then tell you exactly what would get you a yes.

Book a mock interview 

The Human Loop is my newsletter: what I learn day to day as a senior AI engineer. You'll also be first to know when the learning hub launches.