You met Monty in the first article in this series. He's the diligent, hard-working, indefatigable avatar who lives in your computer, reads your Python code, and gets things done. When you met Monty, he was in a medium-sized empty room with a few shelves on one wall. You added some boxes, got some books from the Central Library, and even built some adjacent rooms—the functions defined in your program.
But it's now time to go sightseeing and see more of Python City, where Monty lives and works.
This is the third and final article in the series. The article is best read as a continuation of the first and second articles in this series: Monty and The White Room and The Function Room.
You can also watch videos on The Python Coding Place’s YouTube channel covering this analogy.
Overview of the 'Monty and The White Room' series
Part 1 • Monty and The White Room. The first article introduces the analogy and its main character, Monty. It deals with the basic structure of a computer program, including importing modules, creating variables, and names. We also start to understand the concept of namespaces.
Part 2 • The Function Room. In Part 2, we extend the analogy to include defining and calling functions, including function arguments and return values. We also learn more about scope.
Part 3 (this article) • Python City. The series ends by zooming out from the "rooms" we're creating in our code base to look at how they're connected with other parts of the Python ecosystem.
I first introduced this analogy as an interlude chapter in The Python Coding Book: The White Room • Understanding Programming.
A quick author’s note: I’m in the process of releasing my first video course at The Python Coding Place called A Python Tale. This is a beginner’s course, so unlikely to be relevant to many readers of The Stack—you’re likely beyond beginners’ stage. But if you know anyone who’s just starting, they can start this video course, A Python Tale, which is completely free.
This is the first of many courses on The Python Coding Place which will cover all levels.
Leaving The White Room
You've seen Monty spend a lot of time in the White Room. But he's also constantly popping in and out of this room and strolling across Python City. Let's revisit the short programs I used in the earlier posts in this series to describe Monty's activities. Here's one of the first ones:
Monty starts reading the first line, and the first word he reads is print
. He looks around the White Room for any signs of this name. But it's the beginning of the program, and there isn't much in this room. In fact, it's empty, except for a small, red booklet on one of the shelves. It's labelled "built-in".
So, this red booklet is the only place Monty can look. He picks up the book and looks inside. He finds print
on one of the pages.
But what does Monty read on the print
page in the red booklet? I'll get to this shortly, but first, let's apply our knowledge from the second article in the series that dealt with functions.
The name print
refers to a function. Functions are mini-programs, and they're also represented by rooms in this analogy since stuff happens within them. So, there must be a room with a door labelled print
somewhere. But print
is not a function you define in your program. So it's not adjacent to the White Room. There's no door labelled print
in the main White Room leading to this function room. If there was one, Monty would have seen it before picking up the red booklet.
Let's get back to when Monty finds print
in the red "built-in" booklet. Next to the name print
, Monty reads that this name refers to a function, and there's also the print
function room's address in Python City.
Next, Monty sees the parentheses that follow print
in the code. He knows what he needs to do. He'll need to leave the White Room and venture across Python City to find the print
room. But he won't go empty-handed. He'll take the string "Let's get started"
with him.
And he's off, strolling up and down Python City's streets, carrying the string with him. The print
function room's address leads him to one of the oldest parts of the City, right in the City centre. He finds the door labelled print
and enters the room.
Most Python users don't need to know what's in this room. But Monty follows the instructions associated with this room, which lead him to show the string to the user of the program. Once he completes all the tasks in the print
function room, he leaves the room and heads back to the main White Room. Since print
doesn't return anything, he returns empty-handed. But recall that, if needed, he's got the None
object in his pocket!
Monty is back in the White Room. He's completed the first line of code and is ready for the next one.
When Monty reads the second line, he fetches an empty cardboard box, puts the string "Sunday"
inside, and labels the box using the name today
.
He's ready for the final line. He reads print
again, and finds it in the red booklet. But before he leaves for his walk across Python City to find print
, he looks at what's inside the parentheses. He reads the name today
. As he looks around the White Room, he sees a box labelled today
. Monty fetches its contents and takes the object with him as he leaves the room to go to the print
room. He doesn't take the box labelled today
, only its contents.
Importing Modules
Let's look at a second short program:
In the first article, you read how Monty leaves the White Room when he reads import
and goes to the Central Library. He fetches a book called random
and brings it back to the White Room, where he places it on one of the shelves with the spine facing outward, clearly showing the name random
.
Monty reads print
on the second line, a name he finds in the red booklet. Then he looks inside the parentheses. The first name he reads is random
. He looks around in the White Room and sees the name random
on the spine of a book. He picks the book from the shelf.
The dot which follows the name random
tells Monty to open the book and look for the next word, randint
, within the random
book.
He finds a page in the random
book with the entry for randint
. This page tells Monty that randint
is a function, and it also gives him the address for the function room for randint
.
You can see where this is going already! Or rather, you can see where Monty is going.
Monty takes two objects with him, the integers 1
and 10
, and walks to the Random Quarter in Python City, the district which contains all the rooms referenced in the random
book. He finds a door labelled randint
, goes in, and performs all the actions required in that room.
Monty leaves the room carrying another integer. He takes this new integer, the result of random.randint()
, to the White Room. If this function was used in an assignment, Monty would store this integer in a box in the White Room. Instead, as soon as he's back to the White Room, he leaves again to walk to the print
room and takes this new integer with him.
A Growing Metropolis
Python City started off as a small village and then grew into a town and then a city. There's always some construction work happening in Python City as new districts are built. The old city centre is also well-maintained with improvements happening all the time. Python is now becoming a large metropolis which can deal with every need!
And this city is replicated in each and every computer running Python. Indeed, there are often multiple cities in a single computer! Monty has clones everywhere, all just as diligent and hard-working and ready to spring into action when required.
Code in this article uses Python 3.12
Stop Stack
#36
The Python Coding Place has moved on to the next stage, with a new website (almost) and the next new set of resources that I started to roll out: video courses. I started releasing the first course this week. It's called A Python Tale and it's designed for beginners. But I have many other courses at advanced stages of planning and preparation, so you can expect many new courses at The Place over the coming months for beginners and intermediate learners. If you know of any beginners starting to learn to code, you can recommend A Python Tale. I'll say more about membership of The Python Coding Place in future Stop Stacks.
Recently published articles on The Python Coding Stack:
This Page Is Intentionally Left Blank • The Story of
None
(Paid article) Understanding Python'sNone
The Function Room (Monty and The White Room Series #2) Part 2 in the Monty and The White Room Series • Understanding functions
Monty and The White Room (Monty and The White Room Series #1) Understanding a Python program through The White Room analogy • Part 1
5:30am • Timezone Headaches (Part 1) I need Python's help to figure out the time of my talk • Dealing with timezones and daylight saving with Python's
zoneinfo
anddatetime
modules • The first article in a two-part mini-seriesA Slicing Story Are you sure you know everything there is to know about Python's
slice
object?
Recently published articles on Breaking the Rules, my other substack about narrative technical writing:
The Selfish Reason (Ep. 13) Another reason for authors to innovate • Enjoying the writing process
The Consequential Detail (Ep. 12). Can a single letter or one blank line make a difference? (Spoiler Alert: Yes)
The Unexpected Audience (Ep. 11). What I'm learning from listening to Feynman's physics lectures
The Story So Far (Mid-Season* Review). Are you back from your holidays? Catch up with what you've missed
Broken Rules (Ep. 10). Let's not lose sight of why it's good to break the rules—sometimes
Frame It • Part 2 (Ep. 9). Why and when to use story-framing
Stats on the Stack
Age: 6 months, 2 weeks, and 3 days old
Number of articles: 36
Subscribers: 1,221
Each article is the result of years of experience and many hours of work. Hope you enjoy each one and find them useful. If you're in a position to do so, you can support this Substack further with a paid subscription. In addition to supporting this work, you'll get access to the full archive of articles and some paid-only articles.