Home > Article > Backend Development > What does Zen of Python mean?
Everyone who has used Python basically knows that typing import this in the interactive interpreter will display Tim Peters's The Zen of Python, but its verse-like The statement is a bit confusing, so I wanted to share my experience with it, along with my translation.
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!
Translation and explanation
Zen of Python by Tim Peters
Beautiful is better than ugly (Python aims to write beautiful code)
Clearness is better than obscurity (beautiful code should be clear, with naming conventions and similar styles)
Simplicity is better than complexity (beautiful code should be simple and should not have complex internal implementations)
Complexity is better than clutter (if complexity is unavoidable, there should be no difficult-to-understand relationships between codes, and the interface should be kept simple)
Flatness is better than nesting (beautiful code should be flat , there can’t be too much nesting)
Spacing is better than compactness (beautiful code has appropriate spacing, don’t expect one line of code to solve the problem)
Readability is very important (beautiful code be readable)
Don't violate these rules (these rules are paramount) even in the name of the practicality of exceptions
Don't tolerate all errors unless you absolutely need to (precisely Catch exceptions, do not write except:pass style code)
When there are multiple possibilities, don’t try to guess
Instead try to find one, preferably the only obvious one Solution (if not sure, use exhaustive method)
Although this is not easy, because you are not the father of Python (Dutch here refers to Guido)
It may be better to do it than not to do it , but doing it without thinking is worse than not doing it (think carefully before doing it)
If you can't describe your plan to others, it is definitely not a good plan; vice versa (plan evaluation criteria)
Namespace is a wonderful idea, we should make more use of it (advocacy and call)
The above is the detailed content of What does Zen of Python mean?. For more information, please follow other related articles on the PHP Chinese website!