PYTHON SENSEI: The Zen of Python

--Originally published at Coding The Future

Well hello there, fellow Python learners! Today, the spirit of one of the great masters of the Python language has inspired me to initiate you into the Python lifestyle. Just like in any discipline or lifestyle, in the Python language, there is a set of basic principles.

This great master, who I previously mentioned is Tim Peters, who in the year 2004 CE found the way to perfection in the Python language. He wrote:

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!

These, my friends, are the ideology on which the Python culture rests. Here's a brief explanation of each:

  • Beautiful is better than ugly: Write code that humans can easily read.
  • Explicit is better than implicit: Use boolean types and avoid implicit loops or functions.
  • Simple is better than complex: If it can be done in a simpler way, do it that way.
  • Complex is better than complicated: If something requires more complexity in order to be more efficient, then take it to that level.
  • Flat is better than No need for long names, try to keep object inheritance small.
  • Sparse is better than dense: Be wise about the number of arguments within a method. Don't try to put too much code in a single line.
  • Readability counts: Take the syntax and documentation seriously. Less punctuation, more indentation. Make relevant comments.
  • Special cases aren't special enough to break the rules: Everything is an object.
  • Although practicality beats purity: Sometimes the rules need to be broken: use not only OOP, but Procedural and Functional programming as well.
  • Errors should never pass silently: Fix all fatal errors. They are not the user's fault.
  • Unless explicitly silenced: Catch exceptions with try:except. Process, convert, ignore.
  • In the face of ambiguity, refuse the temptation to guess: Be clear in your code. Remember to keep it logic.
  • There should be one –and preferably only one– obvious way to do it: Erase code that is not being used. Write code that's easy to learn and easy to remember.
  • Although that way may not be obvious at first unless you're Dutch: Master Guido van Possum applied the teachings and created an if-then-else for Python. Use it then.
  • Now is better than never: Fix the problem now. Don't leave your code unfinished.
  • Although never is often better than right now: There are things that are just never going to happen in Python, like adding ? identifiers.
  • If the implementation is hard to explain, it's a bad idea: Don't do it if you can't even explain it yourself.
  • If the implementation is easy to explain, it may be a good idea: If people don't have any trouble understanding it, go ahead!
  • Namespaces are one honking great idea -- let's do more of those!: This is one of Python's greatest features. They follow the order of locals > nonlocals > globals > builtins.

I hope this lesson provides you with the tools and wisdom to dominate Python. And now, if you ever forget these, just run import this on your console and the spirit of the great Sensei will come to you.

In fact, why don't you give it a try right now?

That's all for today, fellow students! Namaste.
@emamex98

--Sources:
Introduction to the Zen of Python
Zen of Python
What do different aphorisms in the Zen of python mean

-- Picture Attributions:
Zen Garden by Wesley Nitsckie
Python Logo by the Python Language Foundation