If your story is good, the computer will execute it efficiently, if the story sucks, the execution sucks.

I write code like I would write a technical paper, it has direction and flow, and you can tell when you have reached the conclusion of the story.

Cute! And true:

I’ve had good results with this “narrative” approach. It works especially well for single-file automation or data crunching scripts. And it helps you keep your code organized and maintainable, even as a project grows.

Let’s take a look at how this would work in practice. We’ll first lay out the logical flow for an example program and then we’ll compare different ways to implement this narrative in Python.

Breaking Down the “Program Narrative”

Imagine the following high-level logical flow for a simple report generator program:

Read input data

Perform calculations

Write report

Notice how each stage (after the first one) depends on some byproduct or output of its predecessor:

Read input data

Perform calculations (based on input data)

Write report (based on calculated report data)

The way I see it, you have two choices here: You can either implement this logical flow from the top down or from the bottom up.

“Top-Down” vs “Bottom-Up” Code Layout

If write your program bottom-up, your function layout will match the logic flow—it’ll go from the fully independent building blocks to the ones that depend on their results.

Python Decorators: A Step-By-Step Introduction – Understanding decorators is a milestone for any serious Python programmer. Here’s your step-by-step guide to how decorators can help you become a more efficient and productive Python developer.

Working With File I/O in Python – Learn the basics of working with files in Python. How to read from files, how to write data to them, what file seeks are, and why files should be closed.

How to Reverse a String in Python – An overview of the three main ways to reverse a Python string: “slicing”, reverse iteration, and the classic in-place reversal algorithm. Also includes performance benchmarks.

🐍🐍 Peer-to-Peer Learning for Pythonistas PythonistaCafe is an invite-only, online community of Python and software development enthusiasts helping each other succeed and grow: » Learn more at pythonistacafe.com