Effective Python 81 - 85

Click here for the first post, which contains the context of this series.

Item #81: Use tracemalloc to understand memory usage and leaks.

  • It can be difficult to understand how Python programs use and leak memory.
  • The gc module can help you understand which objects exist, but it has no information about how they were allocated.
  • The tracemalloc built-in module provides powerful tools for understanding the sources of memory usage.

Item #82: Know where to find community-built modules.

  • The Python Package Index (PyPI) contains a wealth of common packages that are built and maintained by the Python community.
  • pip is the command-line tool you can use to install packages from PyPI.
  • The majority of PyPI modules are free and open source software.

Item #83: Use virtual environments for isolated and reproducible dependencies.

  • Virtual environments allow you to use pip to install many different versions of the same package on the same machine without conflicts.
  • Virtual environments are created with python -m venv, enabled with source bin/activate, and disabled with deactivate.
  • You can dump all of the requirements of an environment with python3 -m pip freeze. You can reproduce an environment by running python3 -m pip install -r requirements.txt.

Item #84: Write docstrings for every function, class, and module.

  • Write documentation for every module, class, method, and function using docstrings. Keep them up-to-date as your code changes.
  • For modules: Introduce the contents of a module and any important classes or functions that all users should know about.
  • For classes: Document behavior, important attributes, and subclass behavior in the docstring following the class statement.
  • For functions and methods: Document every argument, returned value, raised exception, and other behaviors in the docstring following the def statement.
  • If you're using type annotations, omit the information that's already present in type annotations from docstrings since it would be redundant to have it in both places.

Item #85: Use packages to organize modules and provide stable APIs.

  • Packages in Python are modules that contain other modules. Packages allow you to organize your code into separate, non-conflicting namespaces with unique absolute module names.
  • Simple packages are defined by adding an __init__.py file to a directory that contains other source files. These files become the child modules of the directory's package. Package directories may also contain other packages.
  • You can provide an explicit API for a module by listing its publicly visible names in its __all__ special attribute.
  • You can hide a package's internal implementation by only importing public names in the package's __init__.py file or by naming internal-only members with a leading underscore.
  • When collaborating within a single team or on a single codebase, using __all__ for explicit APIs is probably unnecessary.