Search code examples
pythoninterpreterlanguage-implementation

Location of methods and classes in Python3 interpreter


Our professor told us today that we can build an iterator, e.g.

class IteratorExample:
    def __init__(self, n):
       pass

    def __iter__(self):
        return self

    def __next__(self):
        if cond:
            pass
        else:
            raise StopIteration

and that when we will use it, as in (for i in iteratorExample), the interpreter will call the __iter__(self) and __next__(self) methods. My questions are:

  1. If I open C:\Python folder - where can I see the code that calls these methods?
  2. In the python folder, where can I see the code of other built-in methods (like len(), or int()?

Solution

  • You could look at the Python byte code disassembly, to see how for-loop is implemented in Python:

    >>> import dis
    >>> dis.dis('for x in it: pass')
      1           0 SETUP_LOOP              14 (to 17)
                  3 LOAD_NAME                0 (it)
                  6 GET_ITER
            >>    7 FOR_ITER                 6 (to 16)
                 10 STORE_NAME               1 (x)
                 13 JUMP_ABSOLUTE            7
            >>   16 POP_BLOCK
            >>   17 LOAD_CONST               0 (None)
                 20 RETURN_VALUE
    

    Even without looking at the source, we could guess that __iter__ is called by GET_ITER op code and __next__ is called in FOR_ITER.

    And indeed, CPython's Python/ceval.c confirms it e.g., GET_ITER calls PyObject_GetIter(iterable) that is equivalent to iter(iterable) that can call iterable.__iter__() method.


    In the python folder, where can I see the code of other built-in methods (like len(), or int()?

    These functions are also implemented in C (in CPython). You can see them in the CPython source repository.

    Built-in methods are from builtins module that is implemented in Python/bltinmodule.c e.g., len() calls PyObject_Size().

    int is a class for integers in Python. It is implemented in Objects/longobject.c (Python 3).


    Isn't the CPython code in the Python folder?

    No. The Python installation folder does not contain the source code for CPython. It may contain pure Python modules from the standard library such as Lib/fractions.py unless they are zipped or only compiled modules such as .pyc, .pyo files have been installed.

    To get the full source code, run:

    $ hg clone https://hg.python.org/cpython
    

    where hg is the Mercurial executable.

    As an exercise, you could locate where other Python implementations such as Pypy, Jython define GET_ITER, FOR_ITER, len(), int().