In Luciano Ramalho's Fluent Python, an iterable is defined as an object in which the __iter__ method is implemented, with no additional characteristics.
I am currently working out a tutorial for laymen in which I am trying to chunk the core concepts of Python to make programming more manageable for newcomers.
I find it easier to explain iterables and their utility for these people when I associate these objects with the concept of "size" (thus also length). By saying that "iterables are objects that have length" and thus tying in with the len function, I am able to naturally evolve the concept of loops and iteration with commonly used types such as the Standard Library list, dict, tuple, str, as well as numpy.ndarray, pandas.Series and pandas.DataFrame.
However, since now I know about the sole necessity for the __iter__ method, there can be cases where the analogy with len fails. Ramalho even provides an impromptu example in his book:
import re
import reprlib
RE_WORD = re.compile(r'\w+')
class Sentence:
def __init__(self, text):
self.text = text
def __repr__(self):
return 'Sentence(%s)' % reprlib.repr(self.text)
def __iter__(self):
for match in RE_WORD.finditer(self.text):
yield match.group()
As expected, any instance of Sentence is an iterable (I can use for loops), but len(Sentence('an example')) will raise a TypeError.
Since all the aforementioned objects are iterables and have a __len__ method implemented, I want to know if there are relevant objects in Python which are iterables (__iter__), but do not have lengths (__len__) so if I can determine whether I just add a footnote to my tutorial or work out a different analogy.