thermondo

thermondo Developer Handbook

View My GitHub Profile

Python - Style Guide and Good Practices

General

It’s generally good to keep The Zen of Python in mind when writing code.

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

Format

No need to worry about formatting your code. We use black.

Naming

Type Hints

Please use type hints for new code, and feel free to add them to existing code.

Arguments and Keyword Arguments

Mutable vs. Immutable Types

External Python Packages

It’s a balancing act to find the perfect amount of external dependencies. Keep these things in mind:

Remember to verify open source licenses of external packages to make sure commercial use is not restricted or forbidden.

Docstrings

Yes, please! Docstrings are a great way to help manage a long-lived complex code base. We would like to see docstrings for all public classes and functions/methods. But quality goes over quantity. There are valid cases where docstrings are unnecessary or redundant.

Classes

Classes should be declared in the following order:

Exceptions to this rule can be made for example for Django classes. In that case, please refer to our Django style guide.