import madness # how to implement mergesort from scratch using only import statements
Channel: North Bay Python
Date: 05/12/2017

George London

https://2017.northbaypython.org/schedule/presentation/15/

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.

About North Bay Python

A single-track conference north of the Golden Gate, focused on community, collaboration, and all things Python.

North Bay Python is a two-day, single-track Python conference held at the Mystic Theatre in Historic Downtown Petaluma, California, over the weekend of December 2 & 3, 2017.

We're a nonprofit conference for professionals, enthusiasts and students alike. We're focused on inclusion, accessibility, diversity, and affordability. Most importantly, we're planning a great lineup of talks from all over the Python ecosystem, with plenty of time to meet new people and develop new ideas.

Our venue, the Mystic Theatre in Downtown Petaluma, is a beautiful example of an early 1900s Vaudeville theatre. You can find over 50 different food and drink options a short walk away, and the nearest hotel is only a block away.

A Python conference north of the Golden Gate


North Bay Python is a single-track conference with a carefully curated set of talks representing the diverse Python community and their different areas of interest.

If a topic is less to your interest, or you've met some people you really want to sit down and chat with, we'll have plenty of areas away from the main theatre to catch up and chat.

Our goal is to keep prices as low as possible. That means we won't be catering lunch. Instead, you can look forward to extra-long lunch breaks you can use to explore all of the great food options around the venue.