The Zen of Python, the well known list of 20 Python aphorisms (of which only 19 are written down), contains a lot of prescriptive advice for what makes "Good" Python code… or it would, if one of them didn't give you permission to ignore the rest of them. Focusing on one piece of the 19 aphorisms, this talk explores the contradictions in the Zen of Python's advice, and how even with prescriptive rules, writing "Good" Python remains a subjective exercise.
When you type “import this” into a Python interpreter, you get PEP-20, 20* aphorisms that describe what “Good” Python code looks like. Most of these are prescriptive, and have been used to settle countless arguments about how Python should grow, or how new ideas should be implemented. But in these 20 aphorisms (which you may know as the “Zen of Python”), there are some inherent contradictions. When “explicit is better than implicit” and “simple is better than complex”, what does it mean when being explicit means you must introduce complexity? When “readability counts” and “special cases aren’t special enough to break the rules”, what does it mean when making code more readable presents a special case in your codebase? The answer lies in one often-forgotten aphorism: “practicality beats purity”. When you can weigh practicality as a factor, you can decide that other factors are restrictive, and discard them as “impractical”. This idea shows up in many places in Python: Type hints can help you to be explicit, but they introduce complexity. Focusing on practicality means you can adopt type hints only when you need them. Decorators are simple, but can be used to introduce ambiguity. In many cases, the practical benefits of decorators outweigh their potential for misuse. But how far can the pursuit of practicality let you go? Does practicality let you treat the other 19 aphorisms as optional? Does practicality give you an escape hatch from the rest of the Zen of Python? In this talk, we’ll look at how practicality has been a guiding principle in the evolution of Python. We’ll look at how to weigh practicality in the face of the other 19 aphorisms of PEP-20, and we’ll look at how focusing on practicality can guide you towards these other attributes of good Python. --- (* per PEP-20, the Zen of Python contains “20 aphorisms, only 19 of which have been written down.”)
Speakers: Christopher Neugebauer