Coding Style

Contents

Coding Style#

Reading your own Python code should be easy. Even after many years. By reading your code understanding SHOULD not be a painful experience. Also when collaborating with others, e.g. when your put your code on a public repository to benefit others of your work, having a consistent coding style increases the chance that someone is willing to improve your code or extend it.

Python code is read more often than it is written. So simple readable code is far better than ingenious code construction that are hard to understand.

Zen of Python#

The most simple rules are the best. So an easter egg is included in Python to remind you of simple coding.

When you execute:

   import this

The following rules appear:

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!