# Introduction to the Python Interpreter, Part 1: Function Objects

DevFeed: [Introduction to the Python Interpreter, Part 1: Function Objects](<https://devfeed.tech/articles/introduction-to-the-python-interpreter-part-1-function-objects-29435.md>)

Original publisher: [Read original article](<http://akaptur.github.com/blog/2013/11/15/introduction-to-the-python-interpreter/>)

Published: 2013-11-16T02:50:00Z

Content type: tutorial

Language: en

Sources: [Allison Kaptur](<https://devfeed.tech/sources/allison-kaptur.md>)

Topics: [Python](<https://devfeed.tech/topics/python.md>), [Parsing](<https://devfeed.tech/topics/parsing.md>), [Code](<https://devfeed.tech/topics/code.md>), [Compiler](<https://devfeed.tech/topics/compiler.md>)

Tags: [bytecode](<https://devfeed.tech/tags/bytecode.md>), [code](<https://devfeed.tech/tags/code.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [parsing](<https://devfeed.tech/tags/parsing.md>), [python](<https://devfeed.tech/tags/python.md>), [syntax](<https://devfeed.tech/tags/syntax.md>), [tokens](<https://devfeed.tech/tags/tokens.md>)

## AI overview

An introductory tutorial on Python interpreter internals. It explains how Python code is lexed, parsed into an abstract syntax tree, compiled into code objects, and interpreted, with the discussion focused on Python 2.7 and noting similarities with Python 3.

## Source excerpt

[Edit: A significantly expanded version of this series appears as a chapter in The Architecture of Open Source Applications, volume 4, as A Python Interpreter Written in Python.] Over the last three months, I've spent a lot of time working with Ned Batchelder on byterun, a python bytecode interpreter written in python. Working on byterun has been tremendously educational and a lot of fun for me. At the end of this series, I'm going to attempt to convince you that it would be interesting and fun for you to play with byterun, too. But before we do that, we need a bit of a warm-up: an overview of how python's internals work, so that we can understand what an interpreter is, what it does, and what it doesn't do. This series assumes that you're in a similar position to where I was three months ago: you know python, but you don't know anything about the internals. One quick note: I'm going to work in and talk about Python 2.7 in this post. The interpreter in Python 3 is mostly pretty similar. There are also some syntax and naming differences, which I'm going to ignore, but everything we do here is possible in Python 3 as well. How does it python? We'll start out with a really (really) high-level view of python's internals. What happens when you execute a line of code in your python REPL? 1 2 3 4 5 ~ $ python Python 2.7.2 (default, Jun 20 2012, 16:23:33) [GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> a = "hello" There are four steps that python takes when you hit return: lexing, parsing, compiling, and interpreting. Lexing is breaking the line of code you just typed into tokens. The parser takes those tokens and generates a structure that shows their relationship to each other (in this case, an Abstract Syntax Tree). The compiler then takes the AST and turns it into one (or more) code objects. Finally, the interpreter takes each code object executes the code it represen