import madness # how to implement mergesort from scratch using only import statements
By George London

You should never do it, but it’s possible to implement an entire mergesort algorithm in Python from scratch using only import statements. By walking you through how it’s done, I’ll trick you into understanding what actually happens when you import in Python and teach you how to banish ImportErrors.

Sunday 5 p.m.–5:25 p.m.

Recently I had trouble importing a module. So I started actually reading the documentation of the Python import system and realized that I had been writing Python for years without understanding basics like “a module is a file” or “importing a module executes it”.

A few days later I had a terrible realization – it’s possible to implement a mergesort algorithm in Python using only the import statement.

Actually making it work requires learning (and subverting) almost all of the import system’s underlying machinery. So walking through how it’s done creates an excellent (and entertaining) opportunity to shine some light on the darker corners of a system that Python programmers use nearly every day but can work for years without ever really understanding.

This talk starts off with an explanation of the basic fundamentals of the import system, i.e.

1) What does the word “module” actually mean? 2) How does Python find the source code I’m importing? 3) What is Python actually doing when I import code? How is it different from compiled languages? 4) How Python uses namespaces to manage scope and keep our functions and variables organized!

From there, things get pretty weird.

The rest of the talk explains how the import system alone gives us the tools to emulate function evaluation and how that means that we can (but shouldn’t!) write algorithms using import statements instead of functions.

I describe the classic mergesort algorithm and then I show how to actually implement it using imports (under the theory that nothing helps retain knowledge like watching it applied to a trainwreck.)

I briefly discuss how “computation” is an abstract process that can be accomplished in more ways that we might intuitively realize. And then I wrap up with some practical advice on how users can avoid ImportErrors and a warning about the serious but non-obvious security risks of importing Python code.

George London

George is a data engineer at Survata, a survey startup in sunny San Francisco. He’s a self-taught programmer and Python was his 0th language. He likes philosophy, programming, epistemology, and finding ways to break them.