Example code for Effective Python LiveLessons
These items are numbered differently than in the book. Below are links to each item's corresponding piece of example code.
To run all the code for an item, just type ./item_01.py into your shell and see what it prints out. Alternatively you can type python3 item_01.py or python2.7 item_03_example_03.py to run a specific version of Python.
- "Item 1 Know how to slice sequences"
- "Item 2 Avoid using start, end, and stride in a single slice"
- "Item 3 Prefer enumerate over range"
- "Item 4 Use zip to process iterators in parallel"
- "Item 5 Avoid else blocks after for and while loops"
- "Item 6 Take advantage of each block in try/except/else/finally"
- "Item 7 Consider contextlib and with statements for reusable try/finally behavior"
- "Item 8 Use list comprehensions instead of map and filter"
- "Item 9 Avoid more than two expressions in list comprehensions"
- "Item 10 Consider generator expressions for large comprehensions"
- "Item 11 Consider generators instead of returning lists"
- "Item 12 Be defensive when iterating over arguments "
- "Item 13 Know how closures interact with variable scope" and example for Python 2
- "Item 14 Accept functions for simple interfaces instead of classes"
- "Item 15 Reduce visual noise with variable positional arguments"
- "Item 16 Provide optional behavior with keyword arguments"
- "Item 17 Enforce clarity with keyword-only arguments"
- "Item 18 Use None and docstrings to specify dynamic default arguments"
- "Item 19 Prefer helper classes over bookkeeping with dictionaries and tuples"
- "Item 20 Use plain attributes instead of get and set methods "
- "Item 21 Prefer public attributes over private ones"
- "Item 22 Use @classmethod polymorphism to construct objects generically"
- "Item 23 Use subprocess to manage child processes"
- "Item 24 Use threads for blocking I/O, avoid for parallelism"
- "Item 25 Use Lock to prevent data races in threads"
- "Item 26 Use Queue to coordinate work between threads"
- "Item 27 Consider concurrent.futures for true parallelism"
- "Item 28 Use virtual environments for isolated and reproducible dependencies" has no example code
- "Item 29 Test everything with unittest and a directory of tests"
- "Item 30 Consider interactive debugging with pdb" has no example code
- "Item 31 Profile before optimizing"
- "Item 32 Use tracemalloc to understand memory usage and leaks"