The Magic of Docstrings: A Python Developer’s Tale.

Think Data
3 min readJul 30, 2023

--

In Python, docstrings are string literals used to document modules, classes, functions, and methods. They serve as human-readable documentation to explain what a particular piece of code does, its purpose, parameters, return values, and any other relevant information. Writing clear and informative docstrings is crucial for code maintainability, readability, and collaboration within a team.

I once read a quote that made me realize the importance of the doc strings and the quote is “Comments are the code for humans”.

Here is a cool story to understand the significance of doc strings,

Once upon a time, in a bustling kingdom of developers, there lived a young programmer named Alice. Alice was a passionate Python developer, and she loved creating magical and powerful code that could solve various problems. Her code was indeed powerful, but there was a problem — it lacked something essential: docstrings!

One day, the king, who was the head of the development team, assigned Alice a critical task. The kingdom was facing a severe dragon infestation, and they needed a special function to help defeat the dragons. The function was expected to take a list of dragon names as input and return the number of dragons that could be defeated.

Eager to impress the king and prove her worth, Alice worked tirelessly day and night to create the perfect function. She managed to craft a function called defeat_dragons, which indeed seemed promising. However, in her haste and excitement, she neglected to include any docstrings to explain how the function worked.

Without docstrings, her fellow developers found it challenging to understand the purpose of her function. They couldn’t figure out the format of the input list, whether it could handle other data types, or how it calculated the number of defeated dragons.

As a result, the function became a source of confusion and frustration. Other developers had to spend valuable time reading through her code, trying to decipher its inner workings. Unfortunately, nobody could risk using her function in their projects, fearing that it might have unintended consequences.

Despite Alice’s efforts to create a powerful solution, the lack of docstrings prevented her from receiving the recognition she deserved. The king was disappointed, not only because the function was undocumented but also because it slowed down the dragon-fighting efforts.

Seeing her struggle, a wise old developer named Merlin approached Alice with some valuable advice. He explained the importance of docstrings and how they could magically transform her code into something truly remarkable. With docstrings, other developers would instantly understand what her function did, what input it expected, and how to use it effectively.

Taking Merlin’s advice to heart, Alice diligently added clear and informative docstrings to her function. She documented the purpose, input parameters, output values, and even included helpful examples to demonstrate its usage.

Now, her defeat_dragons function looked like this:

def defeat_dragons(dragon_names):
"""
Count the number of dragons that can be defeated.

This function takes a list of dragon names as input and calculates the number of
dragons that can be defeated based on their strength and magical powers.

Parameters:
dragon_names (list): A list of strings representing the names of dragons.

Returns:
int: The number of dragons that can be defeated.

Example:
>>> defeat_dragons(["Smaug", "Fafnir", "Draco"])
2
"""
# Implementation details hidden for brevity
pass

With her function now properly documented, something magical happened! Not only did her fellow developers praise her for the elegant and clear code, but the king was thrilled with the improvement. The defeat_dragons function became a favorite tool for dragon slayers across the kingdom.

Alice’s reputation soared, and she was regarded as one of the most reliable and respected programmers in the land. All thanks to the power of docstrings!

And so, dear reader, the story of Alice reminds us all of the importance of docstrings in Python. Just like Alice, we too can harness the magic of docstrings to create code that not only works wonders but also builds a thriving and supportive developer community.

If you are aspiring Data Engineer or a Data Engineer trying to add more weight to your skill bag or even if you are interested in topics like this, please do hit the Follow 👉 and Clap 👏 show your support, it might not be much but definitely boosts my confidence to pump more usecase based content on different Data Engineering tools.

Thank You 🖤 for Reading!

--

--