See at the end of 02-the-language
Python code reads and should read very much like pseudo code.
“Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.”
― Antoine de Saint-Exupéry
- There are no private variables.
- Avoid factories.
- Avoid mixins.
- Avoid more than one layer of inheritance.
Explicit is better than implicit
Python is a very dynamic language. There is almost no limit to your imagination. But with great power comes great responsibility: you got to be careful about how far you go with introducing magic in your code. Take inspiration from Golang, which has almost no magic at all!
getattr
and hasattr
.
class AbstractToasterFactoryClass:
pass
Globals are:
- Difficult to debug (anything can modify them)
- Difficult to reuse outside the library
The standard library is very complete.
The State of the Octoverse 2020 shows that there are far less dependencies in Python projects:
683 median transitive dependencies for npm followed by PHP (70), Ruby (68), and Python (19). All of which can become impacted by one security vulnerability.
map
is builtin.import functools
for some functional toolingitertools
- Use a lib like
toolz
orfuncy
for more functional tools lambda
are a bit annoying, because they're single line.- Decorators are a super useful pattern and help understand how dynamic Python is
Python lacks some functional features:
- Immutable by default
- True anonymous functions
- Tail recursion