The simple answer is: since iter(s)
returns an iterable object.
Longer answer: iter()
looks for the __iter__()
method, but if it does not find it, it tries to build the iterator itself. Any object that supports __getitem__()
with integer indices starting at 0 can be used to create a simple iterator. __getitem__()
is a function behind row / row indexing, for example s[0]
.
>>> "abc".__iter__() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'str' object has no attribute '__iter__' >>> iter("abc") <iterator object at 0x1004ad790> >>> iter("abc").next() 'a'
See here for more details.
source share