3 Python Koans: One Line Riddles to Teach and Learn Python

Test your knowledge of Python syntax through three syntactic oddities!

Peter Barrett Bryan
Python in Plain English

--

“A koan is a surprising and often perplexing phrase that’s used as a meditation tool” [Source]

Often, a syntactically weird, seemingly paradoxical line of code can teach you something important about a programming language.

Photo by Fabrizio Chiagano on Unsplash

….__str__()

What does the statement return?

“Ellipsis”

What it teaches us

Ellipsis are used in a number of ways in Python. Since the language is white space sensitive, you often find a need to put something on a line even though you don’t need it to do anything. Maybe you are writing a few functions, and you know what you want the signatures to be, but you haven’t had a chance to implement the logic. If you want to be able to lint in the meantime, an ellipsis (or pass) is a great placeholder! It is also often used when slicing multidimensional Numpy arrays.

__str__ is a “magic method” that tells us the string representation of an object. When we call print on an object, this is the value that shows up in the console.

This example teaches us is that an Ellipsis isn’t something exotic. It’s a simple object! We can access its magic methods like any other object. Here, the first three dots are the ellipsis, the fourth dot allows us to get an attribute, and the __str__ is the string magic method!

““““““““““““““““““““““““””””””””””””””””””””””””

What does the statement return?

The empty string!

What it teaches us

Python will concatenate two consecutive strings. For instance, “a”“b” returns “ab”. Python also supports triple quote strings for multiline strings. So we can write “““a”””“““b””” to get the same value. This also works with the empty string! As long as our quotation marks are a factor of six (three to open, three to close), we get the empty string.

dir(dir(dir))

What does the statement return?

[‘__add__’, ‘__class__’, ‘__contains__’, ‘__delattr__’, ‘__delitem__’, ‘__dir__’ ‘__doc__’, …]

What it teaches us

dir is an incredibly useful Python built-in that lists the attributes and methods for an object. In this case, we are calling dir on the values returned by dir when called on… dir! All our functions in Python (including our builtins) are “first class”: we can manipulate them just like any other object. In this case, the innermost dir is an argument: we are calling dir on the function handle of dir! dir(dir) returns all the attributes and methods of the function dir: a list including [‘__call__’, ‘__class__’, ‘__delattr__’, ‘__dir__’, ‘__doc__’, ‘__eq__’, ‘__format__’, …]. Well, a list is also just an object, we can call dir on that too!

Give the article a clap if you learned something! Thanks for reading!

More content at plainenglish.io. Sign up for our free weekly newsletter. Get exclusive access to writing opportunities and advice in our community Discord.

--

--

Software engineer with specializations in remote sensing, machine learning applied to computer vision, and project management.