TLDR: Making Integers Iterable in Python
Date: 2020-06-14 Source: https://arpitbhayani.me/blogs/python-iterable-integers
Overview
Dive into Python internals - Modify CPython to make integers iterable! Explore iterators, protocols, and why it's a bad idea. Iterables in Python are objects and containers that could be stepped through one item at a time, usually using a for ... in loop.
Key Points
- Changing Python’s source code and make integers iterable, and
- Why it might be a bad idea to do so?
- The
PyTypeObject: Every object in Python is associated with a type and each type is an instance of a struct named PyTypeObject. - The
tp_iterslot: Among all the slots available, the slot that interests us is the tp_iter slot which can hold a pointer to a function that returns an iterator object.