The Shoken of Python


Re-thinking the 'Zen of Python' in a more inclusive manner


Posted by Pyntheus Programmers on October 02, 2023

If you enjoy this article, support us!



TLDR: The 'Zen of Python' offers an exceptional set of guidelines for intermediate and advanced developers to write good python code. However, I believe these can be a little daunting to new developers, therefore, I offer up a more beginner-friendly version, named the Shoken of Python.

Find a quiet, calm spot. Clear your mind of any thoughts that no longer serve you. Take a long, deep breath in, followed by a long, deep breath out. Now repeat the following mantra:

“I will write better python code. I will write better python code. I will write better python code”

Welcome to the zen state of mind.
 

The Zen of Python

The below snippet is referred to as the 'Zen of Python'. Whilst not being quite the same as the teachings of the great zen masters, to experienced python-ista’s, it’s almost as spiritual. It represents a set of guiding principles for writing clean and effective python code. In my opinion, there’s no better place to begin a discussion about writing less smelly code than here.

Text

Description automatically generated

Top Tip: To view these principles yourself, there’s an 'Easter Egg' (not the smooth, rich, chocolatey goodness … wait, where was I? Ahh ... the 'Zen of Python') within the python standard library, simply open any python REPL (a terminal, a .py code file or as in my case, a jupyter notebook) and write: import this 

The Zen of Python, written by Tim Peters, first appeared in 1999 in an online chat forum, before officially becoming in 2004 a 'PEP' (Python Enhancement Proposal, an official document informing the python community of some sort of change or new feature) – PEP20, if you want to look this up.

This outlines 20 principles by which all code should abide. Now, some of you more eagle-eyed readers out there might say; Wait a cotton-pickin’ minute! There’s only 19 of them! – and for that, I say, bravo, well-spotted! You’ve just stumbled into a little inside joke for python: Tim Peters left the 20th for Guido Van Rossum to fill in – some say he did with whitespace. If you didn’t think that was very funny, fear not, make it to the end of my article and I promise you some better programming jokes!
 

The Shoken of Python

Whilst the Zen of Python provides an excellent framework for writing good python code, in my opinion, it is introduced to most developers far too late – allowing for poorly designed code to creep into many production codebases. The delayed introduction of the Zen of Python is not without good reason however, some of the concepts and wording may be a little daunting to less experienced programmers ... I’m sure many of you are already googling “what the heck is a namespace?!” whilst reading this. In addition, keeping 19 rules in your head whilst designing code as a beginner can be very overwhelming.

I’m a staunch believer that writing well-designed and well-structured code is a key pillar of success that should be ingrained at every stage of a developer’s journey – and none more so than at the very start. So, with that in mind, if this is your first venture into learning how to write less smelly code, allow me to take you by the proverbial hand and lead you through my re-mastered version: five, easy to understand, beginner-friendly principles!

Caveat: I should note that, altering the original writings of Tim Peters, that have stood the test of time and endured through the decades would do a disservice to a masterful piece of work. Therefore, as a mark of respect, I’m going to name my principles The Shoken of Python (Shoken is a Japanese term which refers to the initial ceremony of a zen student committing to a teacher).

  1. Write code that you want to read – code is meant to be written once and read many times, so do the hard yards upfront to express your solution in the simplest possible form.
  2. Code should pass the 'Alien Test' – if your code can’t be explained to someone outside of the programming world, your code still needs more work.
  3. Plan, plan, and plan some more – shift your thinking from writing code as the first step, to writing code as a last step, after diligently planning your project requirements. Take heed of Abraham Lincoln’s words (whether he actually said them or not is an entirely different issue): “Give me six hours to chop down a tree and I will spend the first four sharpening the axe”. I’ll caveat this by also adding Voltaire’s quote (there’s far more evidence that he actually said these words than Lincoln’s quote above): “Don’t let perfect be the enemy of good”.
  4. If you code is going to fail, make it fail fast and fail informatively – consider all edge cases that might cause your code to fail and raise exceptions for them.
  5. Be pragmatic – in the real-world, programmers often need to balance the best approach and the fastest approach to achieve an outcome; so, sometimes (albeit in rare cases), it’s ok to break the rules.

In the following articles, we’ll explore each of these principles in depth. Stay tuned!

And finally – I promised you some better programming jokes to end the article, so here goes …

---------------------------------------------------

What’s the object-orientated way to become wealthy?
Inheritance.

What’s a programmer’s favourite hangout place?
Foo Bar

Why did the programmer quit his job?
Because he didn’t get arrays

3 SQL databases walked into a NoSQL bar.
They soon left because they couldn’t find a table

Knock-knock
Race-condition
Who’s there?


If you enjoy this article, support us!

362 views