Our professor told us today that we can build an iterator, e.g.
def __init__(self, n):
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:
- If I open C:\Python folder - where can I see the code that calls these methods?
- In the python folder, where can I see the code of other built-in methods (like len(), or int()?
Best How To :
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)
>> 7 FOR_ITER 6 (to 16)
10 STORE_NAME 1 (x)
13 JUMP_ABSOLUTE 7
>> 16 POP_BLOCK
>> 17 LOAD_CONST 0 (None)
Even without looking at the source, we could guess that
__iter__ is called by
GET_ITER op code and
__next__ is called in
And indeed, CPython's Python/ceval.c confirms it e.g.,
PyObject_GetIter(iterable) that is equivalent to
iter(iterable) that can call
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.,
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
.pyo files have been installed.
To get the full source code, run:
$ hg clone https://hg.python.org/cpython
hg is the Mercurial executable.
As an exercise, you could locate where other Python implementations such as Pypy, Jython define