Cover image: by Dodo Bird from Douban Moment
In May, Beijing’s temperature gradually rises, but people can still feel the kindness of the transition from spring to summer in the early morning and evening. I woke up early on the weekend, the sun was bright, sent my daughter to an unknown exam, and bought some beautiful fruits on the way back. There were blooming flowers by the roadside, which I photographed with my phone for today’s illustration.
Last week, I wrote an article titled “Life is Short, I Use Python”, and many readers said they wanted more. In fact, that article was already quite long. In this era of severe fragmentation, I am still very happy that so many readers are willing to spend time reading a nearly 5000-word technical article. In fact, for any programming language, it is impossible to clearly explain its history, development, and architecture in just a few articles, let alone the details.
Physical experiments tell us that the world is a continuous, ever-moving flow of objects, a process of fleeting entities constantly appearing and disappearing, a series of oscillations. The development of a programming language is similar; there are always some fleeting “Flash Points” that influence its development, representing certain characteristics that can either advance or hinder progress. If you are interested, we can discuss some interesting “points” in the Python language later.
The Zen of Python
Seeing this title, you might think I am going to write something esoteric, or perhaps an article on “Zen and the Art of Motorcycle Maintenance”. Those who understand the history of Python know that The Zen of Python is not my creation, but rather that of Tim Peters. On August 19, 2004, Tim added a poetic verse in the Python Shell to explain what programming Zen is. Readers who want to see this text can open the terminal, enter Python interactive mode, and type “import this” to get the following text:
>>> 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!
Let me try to translate this (Peach’s translation):
The Zen of Python
Beauty is better than ugliness. Clarity is better than obscurity. Simplicity is better than complexity. Flat is better than nested. Sparse is better than dense. Readability is important. A special case is not special enough to break the rules, even if that special case is practical. Do not ignore any errors unless you are sure you want to. When there are multiple possibilities, do not try to guess. Find one – and preferably only one – obvious way to solve the problem, although this may not be easy because you are not the father of Python (Guido is Dutch). Now is better than never. Although never is often better than doing it right now. If an implementation is hard to explain, it is a bad idea. If an implementation is easy to explain, it may be a good idea. Namespaces are a great idea – let’s use more of them!
These poetic words express the programming Zen of Python, which is: code should be beautiful, readable, simple, spaced out, handle exceptions properly, avoid using except: pass indiscriminately, find the best solution, think clearly before coding, but do not overthink and end up doing nothing. Practice leads to true knowledge, and good code should be easy to explain to others, and vice versa. Use Python’s namespaces reasonably, and so on.
If you want to enter the world of Python programming, I hope you read this text carefully. Although you may not remember everything, one day you will look back and savor the Zen of Python.
If you would like this series to continue, please let me know.
Gentle pressure, cold appreciation