Photo by Mitchell Luo from Pexels

How to DRY your Python code

Did I ever tell you the definition of insanity ?

Keno Leon
8 min readNov 28, 2021

--

DRY ( Don’t Repeat Yourself ) is generally considered to be a good and important practice for writing good code which I mostly agree with* and while there are tons of folks that will shout at you that your code is not DRY, I have yet to find anybody that tells you how to DRY your code while they are shouting at you, so here we are trying to make amends for all those mean DRY comments !

👋👋 Hi there 👋👋 all my content is free for Medium subscribers, if you are already a subscriber I just wanted to say thank you ! 🎉 If not and you are considering subscribing, you can use my membership referral link, you will be supporting this and other high quality content, Thank you !⭐️⭐ Subscribe to Medium ! ⭐️⭐️

The Problem/Situation

Before getting into the why let’s start with the what, here’s a trivial example of verbose/non DRY code and the same code but DRY:

Both snippets should give you the following printout:

--

--