Python Best Practices: Imports, Mutable Defaults, and Exception Handling per Google Style Guide
This content covers Python coding conventions from Google's internal style guide, focused on maintainability principles: explicit namespace resolution (importing modules rather than individual names to preserve traceability of identifiers), separation of importable module-level code from executable entry points via a guarded main function, and controlled use of comprehensions limited to cases preserving readability. It also addresses the semantics of default argument evaluation (bound once at function-definition time rather than per-call, creating shared mutable state across invocations), the appropriate use of properties versus explicit getter/setter methods based on computational complexity and predictability of attribute access, and the scoping rules governing variable assignment within nested function closures. Finally, it outlines exception-handling conventions: preferring built-in exception types or clearly named custom exception classes over assertions for enforcing correct usage, avoiding broad catch-all exception handling, minimizing the scope of try blocks to limit unintended exception capture, and using cleanup mechanisms (finally clauses or context managers) for resource management.
Python Best Practices: Imports, Mutable Defaults, and Exception Handling per Google Style Guide
This content covers Python coding conventions from Google's internal style guide, focused on maintainability principles: explicit namespace resolution (importing modules rather than individual names …