Skip to content

Let's Add a New Keyword to Python3

Photo of Andrew Kelleher
Hosted By
Andrew K.
Let's Add a New Keyword to Python3

Details

As I'm doing research on the yield keyword; I'm realizing it's actually not extremely difficult to add a new keyword to the language. Let's add a new keyword. The new keyword is "poop". It takes an atom as the argument and prints it to stderr.

You can check out my pull request to my fork of the cpython repository. We'll loosely follow it as we run through the various components required to add a keyword to the language. You can find a really interesting overview of underlying components in PEP 0339: https://www.python.org/dev/peps/pep-0339/

The pull request is here: https://github.com/akellehe/cpython/pull/2

We'll define the syntax for our keyword in Python's grammar, create type definitions for Python to translate, set opcodes in the compiler, and finally define handlers to interact with the frame/stack.

You'll leave this meetup with a basic understanding of Python's fundamental approach to types, frames, and context/scope.

Join us for beers afterwards at Old Town!

Photo of Deep Dive into Python: Know Thy Interpreter group
Deep Dive into Python: Know Thy Interpreter
See more events
Buzzfeed
111 E 18th Street · New York, NY