PEP 8
lastmod 2025-01-24created 2015-03-069 edits
PEP 8 -- Style Guide for Python Code.
Introduction
This document gives coding conventions for the Python code comprising the standard library in the main Python distribution. Please see the companion informational PEP describing style guidelines for the C code in the C implementation of Python.
This document and PEP 257 (Docstring Conventions) were adapted from Guido's original Python Style Guide essay, with some additions from Barry's style guide.
This style guide evolves over time as additional conventions are identified and past conventions are rendered obsolete by changes in the language itself.
Many projects have their own coding style guidelines. In the event of any conflicts, such project-specific guides take precedence for that project.
Programming Recommendations
For sequences, (strings, lists, tuples), use the fact that empty sequences are false.
Don't compare boolean values to True or False using ==.
Test for membership should be 'not in'
E731: do not assign a lambda expression, use a def
In short,
- Because a lambda expression creates an anonymous function, it becomes tedious to identify the source later when you get an error traceback.
- Embedding a lambda expression in another expression to keep code concise is fine.
- But creating a lambda expression and assigning it to a variable has not a single advantage over def.