One editor for writers, developers, and agents

Most doc tools make you choose: accessible for writers, or git-native for developers. Mintlify's editor does both. Writers get WYSIWYG editing, developers keep their git workflow, and AI agents contribute via MCP. Every change syncs both ways. Your whole team, in one place.

Recursion is an interesting topic - it’s used in more places than you'd think, but there’s a high likelihood you’d never write it yourself. For instance, your JSON parser, file system tools, and linter use it.

When I was first learning how to write code, recursion was one of those “what is going on?” concepts in my data structures class.

Before diving into the code, let’s look at what recursion looks like visually (and this is something you can do right now!).

Visual example of recursion using Google searches

Let’s say we were to search for the newsletter on Google, but we accidentally spelled it wrong.

Google will show us a “Did you mean: python snacks newsletter”, which will direct us to another page with the right results (in this example let’s just pretend that the gif below says that 🙃):

Think of our chain here as: “python sancks newsletter” → “Did you mean…” → “python snacks newsletter”. To abstract it further: A → B → C; no clicks caused the web page to go back to the previous web page.

Google has an Easter egg where if you type “recursion” into Google, we’ll see a “Did you mean: recursion” suggestion pop up. If we click it, we’re led back to the same web page with the same “Did you mean: recursion” option:

If we were to click on the correction Google gives us, our chain would look something like: “recursion” → “Did you mean: recursion” → “recursion” → “Did you mean: recursion” → “recursion”… and this would go on forever until you do something else.

Abstracting it further: A → B → A → B → A → …and so on.

That’s recursion: a function/method calling itself.

Example of recursion in Python

You’ll often see examples of the Fibonacci sequence or factorial as examples of recursion, but I think this adds complexity - let’s use a simple countdown instead.

There are many ways you could write it - I’d write it as a while loop:

n = 10
while n >= 0:
   print(n)
   n -= 1

But with recursion, you’re calling the function from inside of itself:

def countdown(n):
    if n < 0:            # stop the recursion
        return
    print(n)
    countdown(n - 1)     # recursion happens here

Every recursive function needs a base case in order to stop executing. Without a base case, you’ll hit Python’s recursion limit and get a RecursionError. In this example, our base case is if n < 0.

When would you use recursion?

I’ve been using Python for over 10 years, and I can honestly say there was only one niche case where recursion made sense (and I don’t remember what it was). It’s a pattern I generally avoid because of how complicated it can get.

However, recursion shines when the data itself is recursive - think of a JSON file. The json.loads() function is a parser that calls itself every time it hits a { or [ in a JSON file.

In addition, recursion is an acceptable technique when you’ve got a tree or a graph data structure and you need to traverse through it - a file system is a great example of this. Python’s os.walk implements recursion to walk through your file system.

Happy coding!

📧 Join the Python Snacks Newsletter! 🐍

Want even more Python-related content that’s useful? Here’s 3 reasons why you should subscribe the Python Snacks newsletter:

  1. Get Ahead in Python with bite-sized Python tips and tricks delivered straight to your inbox, like the one above.

  2. Exclusive Subscriber Perks: Receive a curated selection of up to 6 high-impact Python resources, tips, and exclusive insights with each email.

  3. Get Smarter with Python in under 5 minutes. Your next Python breakthrough could just an email away.

You can unsubscribe at any time.

Interested in starting a newsletter or a blog?

Do you have a wealth of knowledge and insights to share with the world? Starting your own newsletter or blog is an excellent way to establish yourself as an authority in your field, connect with a like-minded community, and open up new opportunities.

If TikTok, Twitter, Facebook, or other social media platforms were to get banned, you’d lose all your followers. This is why you should start a newsletter: you own your audience.

This article may contain affiliate links. Affiliate links come at no cost to you and support the costs of this blog. Should you purchase a product/service from an affiliate link, it will come at no additional cost to you.

Reply

Avatar

or to participate

Keep Reading