Write Python like an expertSun 17 December 2017 by Moshe Zadka
Ten tricks to level up your Python.
Trick 0 -- KISS
Experts know about the weird dark corners of Python -- but do not use them in production code. The first tip is remembering that while Python has some interesting corners, they are best avoided in production code.
Make your code as straightforward as possible.
Trick 1 -- The power of lists
list, or even humbler
pack a lot of punch --
for those who know how to use it.
It serves, of course, as a useful array type.
It is also a good stack, using
with the correct (amortized) performance characteristic.
.sort() method is sophisticated enough it is one of the
few cases where Python actually broke new theoretical grounds
on a sorting algorithm --
timsort was originally invented for it.
Trick 2 -- The power of dicts
dict, or even humbler
also pack a lot of punch.
While many use string keys, it is important to remember any immutable type is possible as keys, including tuples and frozensets. This helps writing caches, memoizers or even a passable sparse array.
The keyword argument constructor also gives it a lot of power for making simple and readable APIs.
Trick 3 -- Iterators and generators
The iterator protocol is one of the most powerful aspects of Python. Experts understand it deeply, and know how to use it to make code shorter, more readable, more composable and more debuggable.
One of the easiest ways to accomplish it is to write functions that accept an iterator and return an iterator: and remembering that generators are really good syntactic sugar for writing functions which return iterators.
If a code base has a lot of functions that return iterators, the iterator algebra functions in itertools become immediately higher value.
Trick 4 -- Collections
The collections module has a lot of wonderful functionality.
For code that needs defaults,
For code that needs counting,
Trick 5 -- attrs
One thing that is not wonderful about the collections module is the
Trick 6 -- First class functions and types
Return functions. Store them in lists, or dictionaries. Keep classes in a double-ended queue. These are not a "Python does what". These are ways to avoid boilerplate or needless indirections.
Trick 7 -- Unit tests and lint
Experts hate having to waste time. Writing unit tests makes sure they have to fix any given bug only once. Correctly configuring a linter makes sure they do not have to comment on every pull request with a list of nitpicks.
Trick 8 -- Immutability
Immutable data structures, such as those available from the Pyrsistent library, are useful for avoiding a lot of bugs. "Global mutable state is the root of all evil" -- and if you cannot get rid of things being global (modules, function defaults and other things) it is often possible to make them mutable.
Immutable data structures are much easier to reason about, and much harder to make bugs that are hard to find and trigger.
Trick 9 -- Not reinventing the wheel
If something is available as a wheel, don't reinvent it. PyPI has ~125K packages, at times of writing. It is almost certain that it has something that takes care of some of the task you are currently working on.
How to know what's worthwhile?
Follow Planet Python, check Awesome python and, if it is within reach, try to go to Python meetups or conferences. (If it's not, of even if it is, PyVideo has the videos -- but talking to other Python programmers is extremely useful.)