Beyond PEP8 - Best Practices for Beautiful Intelligible Code

  • Raymond Hettinger

Line lengths should probably be 90ish, rather than 79

PEP8-ifying code is distracting, doesn't help code quality that much, scrambles version control history, and can introduce bugs

PEP8 Golden Rule: "PEP8 unto theyself, not unto others"

APIs should be pythonic

  • avoid unneccessary packages, create a single-file module with all the key parts if necessary
  • create custom exceptions
  • if something is iteratble, make it iterable
  • use properties instead of getters/setters
  • make context managers when you have setup/teardown logic
  • define __repr__ to help with debugging
    • reference self.__class__.__name__, rather than the class' name, so subclasses get the right thing

Make pythonic APIs out of the non-pythonic APIs you're given.