Pathetic Programming is a series I started to build absurd programs with Python. With already two blogs in the series namely: Creating a Random Excuse Generator with Python and Are You Even a Real Python Programmer? Take This Useless Quiz to Find Out, I thought it was now time for the third one. And this time, we are making our code talk back to us.
There comes a moment in every programmer’s life when they stare into their Linux CLI and think: “This is great, but what if my code/bug was needlessly polite? What if instead of spitting out >>>
prompts like a bored teacher handing out quizzes, it tried to comfort me about my life choices?”
Maybe that moment has not yet come for you. Maybe you’re still in the stage of your career where you just use Python for, you know, actual work. Data science, web apps, game dev, automation scripts, occasionally Googling “python fizzbuzz solution” because you suddenly blanked during an interview.
But I’m here to drag you with me into the pit of absurdity, where we don’t just write code—we ruin good ideas on purpose.
Welcome to Pathetic Programming 3: The Overly Polite Python Interpreter.
We’re building a wrapper around the Python REPL that doesn’t just run your commands. No, no, that would be far too practical. Instead, it will shower you with compliments, apologies, or sarcastic remarks before it even considers executing your brilliant (or questionable) code.
Brace yourself. The Python shell is about to become way too human.
Table of Contents
The Problem No One Asked to Solve
Python’s REPL is fine. Functional. Efficient. Clinical. It does the job, like a tired librarian who just stamps your overdue books and sends you away with silent judgment.
What it doesn’t do is validate you. It doesn’t gas you up when you import math
like an absolute intellectual, nor does it apologize when you typo pritn
. It just shrugs and throws an error message. Rude.
Imagine instead:
>>> print("Hello World")
Oh wow, stunning choice of words. Truly revolutionary. Executing now:
Hello World
Or:
>>> 1/0
Yikes. Sorry to break it to you, but dividing by zero is still illegal.
Traceback (most recent call last):
...
ZeroDivisionError: division by zero
Suddenly, Python isn’t just a tool. It’s a co-pilot, a hype man, and occasionally, that sarcastic friend who roasts you for writing a while True:
loop at 3AM.
Is this useful? No. Is it necessary? Also no. Will it make you smile during your next debugging spiral? Probably. And that’s all the justification we need.
What Even Is a REPL Wrapper?
For the uninitiated (aka people who still have social lives), Python’s REPL is just its interactive shell. Read, Evaluate, Print, Loop. You type stuff, it runs stuff, it prints stuff, repeat until you run out of caffeine or willpower.
We’re not going to rewrite Python itself (please calm down, C devs). Instead, we’ll just hijack the input/output flow, slap on some personality, and pass everything back to the real interpreter behind the scenes.
Think of it like duct-taping a soundboard to your terminal. Every time you type a command, Python still does its thing—but first, it tells you that you’re special. Or stupid. Or both.
Making Python Talk Back
The good news: Python actually comes with a handy code
module that lets us spin up custom interactive interpreters. It’s like the official devs knew we’d eventually want to ruin their nice clean REPL experience.
Here’s the absolute barebones starting point:
import code
# Create a custom interpreter
console = code.InteractiveConsole()
# Start it
console.interact("Welcome to the Overly Polite Python Interpreter!")
If you run this, congrats—you’ve just spawned a slightly more boring Python shell. It greets you with a nice message, but otherwise behaves the same. That’s our canvas. Now we start vandalizing.
Step 1: Insert Some Sass
The InteractiveConsole
lets us override the runsource
method, which is where the magic happens. Every time you type a command, it goes through here. Normally, it just compiles and executes. But what if we, say, injected an extra line of text first?
import code
import random
class PoliteConsole(code.InteractiveConsole):
def runsource(self, source, filename="<input>", symbol="single"):
# Pick a random remark
remarks = [
"Ah yes, truly a line of code for the ages:",
"Brace yourself—it’s your face, I mean your code:",
"I can’t believe you wrote that. But okay:",
"Solid choice. Bold move. Let’s see what happens:",
"This better not crash everything again:"
]
print(random.choice(remarks))
return super().runsource(source, filename, symbol)
PoliteConsole().interact("Welcome, noble developer.")
Now, when you run commands, Python prefaces them with a snarky or supportive comment. Congratulations, you’ve successfully made Python passive-aggressive.
Step 2: Add Compliments, Apologies, and Sarcasm Buckets
One set of remarks isn’t enough. We want moods. Sometimes Python should cheer you on like a sports coach, sometimes it should apologize for existing, and sometimes it should roast you like you just bombed open mic night.
We can build separate lists and shuffle between them:
compliments = [
"Wow, you’re like the Shakespeare of code.",
"This line is so good, I’m framing it.",
"Honestly, Python should be paying *you* to run this."
]
apologies = [
"I’m so sorry in advance if this explodes.",
"Look, I’ll try my best, but no promises.",
"Apologies if this turns your CPU into a space heater."
]
sarcasm = [
"Oh sure, THAT’S going to work. Definitely.",
"Bold of you to assume I’ll run this without crying.",
"Yes, yes, brilliant. A real Nobel Prize submission."
]
And then, when runsource
fires, randomly pick a category:
moods = [compliments, apologies, sarcasm]
chosen = random.choice(moods)
print(random.choice(chosen))
Every input gets a different emotional vibe. One moment Python is your cheerleader, the next it’s your disappointed mom. Keeps you on your toes.
Step 3: Handling Errors with Extra Sass
Normal Python errors are fine, but we can do better. Imagine this instead of the usual:
>>> 1/0
Wow, okay, dividing by zero. Bold move, champ.
Traceback (most recent call last):
...
ZeroDivisionError: division by zero
We can hijack showsyntaxerror
and showtraceback
in InteractiveConsole
to sprinkle in custom reactions before printing the boring technical bits:
class PoliteConsole(code.InteractiveConsole):
def showtraceback(self):
print("Oh no. Oh dear. That didn’t go as planned, did it?")
super().showtraceback()
def showsyntaxerror(self, filename=None):
print("Syntax? Never heard of her. Try again.")
super().showsyntaxerror(filename)
Suddenly, even failure feels… cozy.
Step 4: The Infinite Rabbit Hole
At this point, the core idea works. You’ve got a Python REPL that:
- Reacts to your inputs like a moody sidekick
- Compliments or insults you randomly
- Softens the blow of errors with silly commentary
From here, the sky’s the limit. You can add Easter eggs (detect import this
and respond with “Yes, we know you’re deep”), keyword triggers (if the user types exit()
, Python should say “Fine, abandon me”), or even timed “motivational quotes” when the session runs too long.
For example:
def interact(self, banner=None, exitmsg=None):
print("Welcome to the Polite Python Interpreter!")
print("Type 'exit()' if you’re heartless enough to leave me.")
super().interact(banner, exitmsg="I’ll just… be here, alone.")
Goodbye messages have never felt so guilt-trippy.
Full Code
Being the programmers we are, I am certain many have jumped directly to this section by just reading the heading “Full Code”. So below is the entire code that can be copy-pasted and run. Hey, but while you are here, first follow me on Instagram: @machinelearningsite. Now go nuts with the code:
import code
import random
import sys
class PoliteConsole(code.InteractiveConsole):
def __init__(self, locals=None, filename="<console>"):
super().__init__(locals=locals, filename=filename)
# Define moods
self.compliments = [
"Wow, you’re like the Shakespeare of code.",
"This line is so good, I’m framing it.",
"Honestly, Python should be paying *you* to run this.",
"Chef’s kiss. Truly inspiring.",
"Is it hot in here, or is it just your code?"
]
self.apologies = [
"I’m so sorry in advance if this explodes.",
"Look, I’ll try my best, but no promises.",
"Apologies if this turns your CPU into a space heater.",
"Sorry, did you *mean* to write that?",
"Forgive me if I cry while running this."
]
self.sarcasm = [
"Oh sure, THAT’S going to work. Definitely.",
"Bold of you to assume I’ll run this without crying.",
"Yes, yes, brilliant. A real Nobel Prize submission.",
"Flawless. Truly flawless. (Just kidding.)",
"This is fine. Everything’s fine."
]
self.moods = [self.compliments, self.apologies, self.sarcasm]
def runsource(self, source, filename="<input>", symbol="single"):
# Skip empty lines (otherwise you’ll get random sass for pressing enter)
if source.strip():
chosen = random.choice(self.moods)
print(random.choice(chosen))
return super().runsource(source, filename, symbol)
def showtraceback(self):
print("Oh no. Oh dear. That didn’t go as planned, did it?")
super().showtraceback()
def showsyntaxerror(self, filename=None):
print("Syntax? Never heard of her. Try again.")
super().showsyntaxerror(filename)
def interact(self, banner=None, exitmsg=None):
banner = banner or "Welcome to the Overly Polite Python Interpreter!"
print(banner)
print("Type 'exit()' if you’re heartless enough to leave me.")
super().interact(banner="", exitmsg="Fine. Abandon me. I’ll just sit here alone.")
if __name__ == "__main__":
console = PoliteConsole()
console.interact()
So a bit information about the code above: Instead of silently executing commands, it randomly responds with compliments, apologies, or sarcastic remarks before running your code, turning each line into a mini performance review. It also overrides error handling, softening the blow of syntax and runtime errors with some commentary like…Wait! Why don’t you see for yourself. Go ahead and try it out. Take screenshots, post on Instagram and tag me @machinelearningsite. I would love to see how your code roasts or complements you.
What’s Next
So where do we go from here? Well, if you’ve made it this far, you’ve officially crossed the line from “productive coder” to “agent of chaos,” and honestly, there’s no turning back. The Polite Python Interpreter is just the beginning—think of it as training wheels for more absurd projects. You could add Easter eggs that trigger special roasts when you type in things like import antigravity
or exit()
. You could make the interpreter keep track of how many errors you’ve made and get progressively sassier with each mistake. You could even hook it up to text-to-speech so your laptop whispers “Wow, genius move” every time you forget a colon.
If you’re new to this series (first of all, welcome to the nonsense), don’t forget to head back and check out the first two experiments in Pathetic Programming:
- Creating a Random Excuse Generator with Python
- Are You Even a Real Python Programmer? Take This Useless Quiz to Find Out
And hey—if you’ve got an idea for a gloriously impractical Python project that deserves to exist, I’d love to hear it. Ping me on Instagram at @machinelearningsite and maybe your idea will end up in a future entry.
Stay tuned for the next one, where we’ll once again ignore practicality and chase down something weird, unnecessary, and absolutely delightful.