Page 1: Advanced Function Concepts
*args and **kwargs:
def example(*args, **kwargs):
for arg in args:
print(arg)
for key, value in kwargs.items():
print(f"{key}: {value}")
Use when number of arguments is unknown.
Page 2: Scope and Namespaces
LEGB Rule:
- Local
- Enclosing
- Global
- Built-in
Use global to modify global variables inside functions.
Page 3: Context Managers
with open('file.txt', 'r') as f:
contents = f.read()
Custom context managers using class or contextlib.
from contextlib import contextmanager
@contextmanager
def managed_resource():
print("Setup")
yield
print("Cleanup")
Page 4: Multithreading
import threading
def worker():
print("Working")
t = threading.Thread(target=worker)
t.start()
t.join()
Good for I/O-bound tasks, not CPU-bound (use multiprocessing for that).
Page 5: Useful Built-in Functions
- len(), type(), range(), zip(), enumerate()
- any(), all(), sorted(), reversed()
- isinstance(), id(), dir(), vars()
Help with inspecting and manipulating objects.