Discover more from The Python Coding Stack • by Stephen Gruppetta
Python City (Monty and The White Room Series #3)
Part 3 in the Monty and The White Room Series
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.
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.
I’m grateful to all subscribers on The Python Coding Stack. If you want to support the publication further, you may want to consider a paid subscription.
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
So, this red booklet is the only place Monty can look. He picks up the book and looks inside. He finds
But what does Monty read on the
Let's get back to when Monty finds
Next, Monty sees the parentheses that follow
"Let's get started" with him.
And he's off, strolling up and down Python City's streets, carrying the string with him. The
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
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
He's ready for the final line. He reads
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
today, only its contents.
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. 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
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
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
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
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
The Python Coding Stack • by Stephen Gruppetta is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.
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's
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
datetimemodules • The first article in a two-part mini-series
A Slicing Story Are you sure you know everything there is to know about Python's
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
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.