- Know which version of python you’re using
- Follow the PEP 8 style guide
- Know the differences Between
bytes
, str
, and unicode
- Write helper functions instead of complex expressions
- Know how to slice sequences
- Avoid using
start
, end
and stride
in a single slice
- Use list comprehensions instead of
map
and filter
- Avoid more than two expressions in list comprehensions
- Consider generator expressions for large comprehensions
- Prefer
enumerate
over range
- Use
zip
to process iterators in parallel
- Avoid
else
blocks after for
and while
loops
- Take advantage of each block in
try/except/else/finally
- Prefer exceptions to returning
None
- Know how closures interact with variable scope
- Consider generators instead of returning lists
- Be Defensive when iterating over arguments
- Reduce visual noise with variable positional arguments
- Provide optional behavior with keyword arguments
- Use
None
and Docstrings to specify dynamic default arguments
- Enforce clarity with Keyword-only arguments
- Prefer helper classes over bookkeeping wih dictionaries and tuples
- Accept functions for simple interfaces instead of classes
- Use
@classmethod
polymorphism to construct objects generically
- Initialize parent classes with
super
- Use multiple inheritance only for mix-in utility classes
- Prefer public attributes over private ones
- Inherit from
collections.abc
for custom container types
- Use plain attributes instead of Get and Set methods
- Consider
@property
instead of refactoring attributes
- Use descriptors for reusable
@property
methods
- Use
__getattr__
, __getattribute__
, and __setattr__
for lazy attributes
- Validate subclasses with metaclasses
- Register class existence with metaclasses
- Annotate class attributes with metaclasses
- Use
subprocess
to manage child processes
- Use threads for blocking I/O, avoid for parallelism
- Use
Lock
to prevent data races in threads
- Use
Queue
to coordinate work between threads
- Consider coroutines to run many functions concurrently
- Consider
concurrent.futures
for true parallelism
- Define function decorators with
functools.wraps
- Consider
contextlib
and with
statements for reusable try/finally
behavior
- Make
pickle
reliable with copyreg
- Use
datetime
instead of time
for local clocks
- Use built-in algorithms and data structures
- Use
decimal
when precision is paramount
- Know where to find coummunity-built modules
- Write docstrings for every function, class, and module
- Use packages to organize modules and provide stable APIs
- Define a root
Exception
to insulate callers from APIs
- Know how to break circular dependencies
- Use virtual environments for isolated and reproducible dependencies
- Consider module-scoped code to configure deployment environments
- Use
repr
strings for debugging output
- Test everything with
unittest
- Consider interactive debugging with
pdb
- Profile before optimizing
- Use
tracemalloc
to understand memory usage and leaks