paint-brush
Cracking 3 Python Easter Eggsby@offcode
922 reads
922 reads

Cracking 3 Python Easter Eggs

by Adam SchmidegNovember 26th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Enough of the boring side of programming, let's see the funny part of Python. We're going to use the console.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Cracking 3 Python Easter Eggs
Adam Schmideg HackerNoon profile picture


Console

Ada: Enough of the boring side of programming, let's see the funny part of Python. We're going to use the console. You may know what happens if you type python spam.py.

Knut: Easy. It will execute a Python file named spam.py.

Ada: Indeed. And what happens if there's no such file?

Knut: An error, I assume.


Ada: Right. Now, what happens if I omit the file completely? I just type python.

Knut: ...

Ada: I get to the intestinal tract of the computer.

Knut: ?

Ada: Look, we get to another console. But this is different as you can tell by its look. Instead of a single >, its prompt is >>> showing we are three times deeper. Whatever you type from now on will be directly interpreted by Python.

Knut: I wouldn't call it the intestines of the computer.

Ada: It's the center of the brain, then. You can type 1+2 and it prints the result. Isn't that amazing?

>>> 1+2
3


Knut: That's elementary math.


Ada: How about 1089*9?


Knut: I don't know. You just open a calculator app. I see this multiplication as the reverse of amazing.


Ada: Gnizama? Or do you mean the number? Then you're right:

>>> 1089*9
9801


Knut: 😮

Ada: You have a few neat functions at your hand. You can quickly add up the numbers from 1 to 1000


>>> sum(range(1, 1001))
5050


Yes, it counts only up to 1000 and excludes the upper limit. But enough of the functions that are too easily available.

Hello

Ada: The best parts in life are tucked away in a secret drawer. Python calls a secret drawer a module. To access its content, you have to import it first.

Knut: You actually type import secret drawer, or what?

Ada: Almost. Module names can contain only alphanumeric characters and underscores, but no spaces. Python actually loves underscores.

Knut: import secret_drawers, then?

Ada: That would be syntactically correct. Unfortunately, there's no such module. But I just found out you can


>>> import secrets


Ada: Note that importing a module is usually silent, it doesn't print anything. Not only in the case of the secrets module.

Knut: You spoke of a secret drawer.

Ada: You access it using the . operator. Let's take a sneak peek into this module.

>>> secrets.token_hex()
'007da1d6b80405afa8db1a5af21ef716316bab08141dc4b21c1d5dccbec13315'


Knut: It even starts with 007 to leave no room for doubt.

Ada: I wanted to talk about something more open and simple.

>>> import __hello__
Hello world!


Knut: Wait, you said importing a module is silent.

Ada: This is an exception. It becomes silent after the first import:

>>> import __hello__
Hello world!
>>> import __hello__


Knut: Why so many underscores? Why not just import hello?

Ada: Hm, I don't know, give me a few minutes:

>>> import time
>>> time.sleep(5 * 60)


Ada: It turns out to be more than an inside joke, it's a test case for an advanced feature. When it was broken in an earlier version of Python, some developers considered it a release blocker.

Antigravity

(Ada is opening the console) Knut: I bet you're about to import another module.

Ada:

>>> import antigravity


Knut: It doesn't print anything... wait, it opens a browser window...

Ada: and it takes you to this XKCD comic I wrote 20 short programs in Python yesterday. It was wonderful. Perl, I'm leaving you. Knut: This "hello, world" again.

Ada: They say when you start learning a new programming language, the first program should print this message. See how you can say 'Hello, World' in 53 Different Programming Languages.


For comparison, here's the same program in Java:

class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}


Knut: I understand the enthusiasm of the flying stick figure. Let me write my first program.

>>> print "Hello World!"
   ...
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?


Knut: This is not exactly what I expected.

Ada: This comic was drawn back when Python was at a young age at version 2. It wasn't very disciplined, it didn't require parents for print. Python has become more mature, they say, it's already version 3, and it's now slightly stricter.

Knut:

>>> print("Hello World!")
Hello World!


Knut: Wow, my first program works! What else can we find in this drawer?


Ada: There's a method to investigate that.

>>> dir(antigravity)
   ...
   'geohash',
   'webbrowser',
   ...


Ada: It lists even the modules used by this module, for example webbrowser which opened XKCD for you.

Knut: What's geohash?

Ada:

>>> help(antigravity.geohash)
    ...
    geohash(latitude, longitude, datedow)
        Compute geohash() using the Munroe algorithm.
    ...


Ada (🤷): Give me a few minutes.

>>> import time, webbrowser
>>> webbrowser("https://duckduckgo.com/?q=geohash+munroe+algorithm+python+antigravity")
>>> time.sleep(5 * 60)


Ada: Munroe is the name of the guy who draws XKCD. I don't understand at least half of his jokes. Luckily, there's an accompanying site that gives the explanations. But even 353: Python - explain xkcd doesn't say a word about this function. Then I found The History of Python: import antigravity where Guido van Rossum, Python's creator himself had added a reference to 426: Geohashing - explain xkcd. The geohash function is an Easter egg within an Easter egg.

Spam

Knut: What will you import next?

Ada: Do you like spam?

Knut: My hate for it burns hotter that the fires of Mount Doom.

Ada: Let's try it then.

>>> import spam


Knut: It prints nothing.

Ada: Wait

>>> spam.system('pwd')
/home/ada/easter-eggs
>>> spam.system('ls')
hello.py
intro.py
spam.py
...


Knut: So it's a module that can run any command on the shell?

Ada: Yes, it's a simple interface to the C library function system().

Knut: It's not a security issue, then?

Ada: You're right, it's not. The documentation suggests it's more of a joke:

>>> print(spam.__doc__)
 This is a dummy implementation of the spam module.

 It exists to show that a package can contain modules that are not
 intended to be directly imported but are defined to be discoverable by
 introspection.

 See the documentation for the builtin __import__() function for more
 information.


Knut: But it's not a joke. It can actually run shell commands.

Ada: I think it's a joke. It's not even a real implementation. It's only a placeholder.


>>> spam.system('cat /usr/lib/python3.6/spam.py')
	Traceback (most recent call last):
	File "<stdin>", line 1, in <module>
	AttributeError: module 'spam' has no attribute 'system'


Ada: It's called spam as a reference to the Monty Python sketch. The name of the programming language is an homage to Monty Python, its origin has nothing to do with the snake.

This

Ada: I want to show you one more thing.

Knut: You promised to crack 3 Easter eggs. This is the fourth.

Ada: Talking about this

>>> import this

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!


Knut: Who's Tim Peters?

Ada: Probably one of the first contributors. I like he didn't write mathematical rules, but took a poetic approach.

Knut: My favorite part is "There should be one-- and preferably only one --obvious way to do it."

Ada: It's a reply to the motto of Perl: There's more than one way to do it. It was another programming language of the time when Python was created. If you hover over the image of Antigravity, you can read "It was wonderful. Perl, I'm leaving you."

Knut: Why is the one way more obvious to the Dutch? Monty Python were a British comedy troupe.

Ada: But Guido van Rossum is Dutch. If you try to

>>> import guido


Knut: You're not telling me...

Ada: No, there is no such module. In fact, one of the modules I introduced doesn't exist, I just made it up. There are really 3 Easter Eggs in this essay and 1 Cuckoo's Egg. It's up to you to find out which is which.