# Introduction to the Python Interpreter, Part 2: Code Objects

DevFeed: [Introduction to the Python Interpreter, Part 2: Code Objects](<https://devfeed.tech/articles/introduction-to-the-python-interpreter-part-2-code-objects-29434.md>)

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

Published: 2013-11-16T03:22:00Z

Content type: tutorial

Language: en

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

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

Tags: [code](<https://devfeed.tech/tags/code.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [function](<https://devfeed.tech/tags/function.md>), [python](<https://devfeed.tech/tags/python.md>)

## AI overview

This installment of a series on the Python interpreter examines function code objects. It explains that a code object is generated by the Python compiler and interpreted by the interpreter, then inspects attributes such as variable names, constants, and argument count.

## 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.] This is part of a series on the python interpreter. Part 1 here. When we left our heroes, they were examining a simple function object. Let's now dive a level deeper, and look at this function's code object. 1 2 3 4 5 6 7 8 >>> def foo(a): ... x = 3 ... return x + a ... >>> foo <function foo at 0x107ef7aa0> >>> foo.func_code <code object foo at 0x107eeccb0, file "<stdin>", line 1> As you can see in the code above, the code object is an attribute of the function object. (There are lots of other attributes on the function object, too. They're mostly not interesting because foo is so simple.) A code object is generated by the Python compiler and intepreted by the interpreter. It contains information that this interpreter needs to do its job. Let's look at the attributes of the code object. 1 2 3 4 5 6 7 >>> dir(foo.func_code) ['__class__', '__cmp__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'co_argcount', 'co_cellvars', 'co_code', 'co_consts', 'co_filename', 'co_firstlineno', 'co_flags', 'co_freevars', 'co_lnotab', 'co_name', 'co_names', 'co_nlocals', 'co_stacksize', 'co_varnames'] There's a bunch of stuff going on here, much of which we're not going to worry about today. Let's take a look at three attributes that are interesting to us for our code object on foo. 1 2 3 4 5 6 >>> foo.func_code.co_varnames ('a', 'x') >>> foo.func_code.co_consts (None, 3) >>> foo.func_code.co_argcount 1 Here are some intelligible-looking things: the names of the variables and the constants that our function knows about and the number of arguments the function takes. But so far, we haven't seen anythi