Python City: Understanding how a Python Program Works (The White Room Series #3)

You’ve written a Python script or a project containing several modules. You press Run, figuratively or literally. What happens behind the scenes in the microseconds or seconds or minutes it takes for your program to run? You can dive into the details about the internal functioning of Python to learn how a Python program works. But there’s another way to visualise what’s happening.

My vision of how a Python program works is what I call The White Room analogy. The computer program is personified as a character called Monty, who spends a lot of time inside the White Room. This room represents the infrastructure in which the program runs.

This blog is the third in a series of three that describe this analogy. I’ll start with a brief recap of the first two parts and then finish off the analogy by introducing Python City!

Recap: The White Room

In the first post in the White Room Series, I introduced the White Room and Monty. If you’ve just read that post, then you can skip the summary in this section and move on.

If you haven’t, here’s a very brief recap. Each blank Python file you create is represented by a room that’s mostly empty except for a few shelves on one of the walls and a small booklet named “built-in”. This booklet has some functions, constants, and other keywords in it.

Monty is a friendly, hard-working character who represents the computer program. He’s very fast and efficient, but you’ll need to spell out instructions clearly when you ask him to do something.

When you ask Monty to create a variable to store some information in, he’ll get an empty box and label it with the variable name you tell him. He’ll place whatever data you want in the box and place the box on one of the shelves.

If you’ve used an import statement, Monty will leave the White Room briefly to go to the library, where he’ll fetch a book with the name of the module you’re importing. He’ll take this book back to the White Room and place it on a shelf.

When you use any name in your script, Monty will look around the room to find that name. It may be a book (a module you’ve imported), it may be a box (a variable you’ve created), or it may be a name that’s inside the “built-in” booklet.

Recap: The Function Room

In the second post of the series, you read about what happens when you define a function within your script. A function is a mini-program and is represented by a separate room—the Function Room—adjacent to the White Room.

The door leading from the White Room to the Function Room has a label on it. The name on this label is the function name.

When you call a function in your program, Monty will find the name of the function on the Function Room’s door. He’ll open the door and go through it. He may need to take some things with him as he goes to the Function Room. These are the arguments in the function call.

Monty performs all the tasks he’s asked to do in the Function Room and then returns to the White Room, possibly bringing some information along with him. These are the items of data returned by the function.

Python City

Let’s complete this analogy by looking at the bigger picture. The White Room and the Function Rooms that are attached to it do not exist in isolation. They’re part of a larger area that contains many more rooms and buildings. This is Python City.

You’ve already come across another building in Python City. When you import a module, Monty leaves the White Room and goes for a stroll in Python City to find the largest building of all—the library. This building contains well over a hundred thousand books that represent the modules you can import. When you install modules on your computer, you’re adding more books to this library.

But what do these “books” really contain?

Let’s look at an example:

import random

my_number = random.randint(1, 5)

print(my_number)

When you run this script, the first instruction Monty receives is import random. He leaves the White Room, goes to the library at the centre of Python City, and looks for the book named random. He borrows the book from the library and takes it back to the White Room.

Python City Neighbourhoods

His second instruction is to bring an empty box and label it my_number. However, on the right-hand side of the equals sign, he reads the following: random.randint(1, 5). He recognises the word random as it’s the name of a book he just brought from the library. He opens this book and looks inside for the name randint, which he finds.

Next to the name randint in the book random, Monty finds the directions for him to find the randint room in Python City. randint() is a function, and therefore, there’s a Function Room labelled randint somewhere in Python City. This Function Room is not adjacent to the White Room as it’s a room that the authors of the random module created.

Monty reads the directions that will lead him to the randint Function Room, picks up a couple of integers (1 and 5), and leaves the White Room to go and find randint, carrying the two integers with him.

The randint Function Room is in a neighbourhood called random. The neighbourhood name is the same as the name of the book borrowed from the library. Monty gets to the random neighbourhood in Python City, and there he finds a small building with just one room with the name randint on the door. He goes in, places the two integers into the boxes that are waiting to be filled (the parameter boxes), and does whatever is required in the randint Function Room.

When he finishes, he leaves the randint room. He’s carrying an integer with him, which is the result that randint() returns. Monty makes his way back to the White Room and places the integer he brought from the randint Function Room in the box labelled my_number.

City Centre (Downtown Python City)

Finally, Monty ready the instruction print(my_number). He looks for the name print in the White Room and he finds it in the “built-in” booklet. Next to the name print in the booklet, he finds the directions to the print Function Room in Python City. However, before leaving the White Room, he brings the box labelled my_number down from the shelf. He doesn’t take the whole box with him, but just its contents. In this case, this will be an integer between 1 and 5.

And Monty goes off for another stroll in Python City to find the print Function Room. This room is located in the city centre or downtown Python City!

Final Words

And that completes the visual picture that I’ve created to help me understand how a Python program works behind the scenes. Like all analogies, it’s not a perfect representation of what really happens, and the analogy will fail if you push it and stretch it too far. But I hope that whatever your proficiency of programming in Python is, whether you’re a relative beginner or an experienced veteran, you’ll find this analogy insightful and that it may inspire you to think a bit more about how a Python program works!

Further Reading


Get the latest blog updates

No spam promise. You’ll get an email when a new blog post is published


Leave a Reply