关于python:为什么可以沿字符串迭代?

Why is it possible to iterate along a string?

我试图理解为什么我可以沿着字符串迭代。我在文档中看到的是:

One method needs to be defined for container objects to provide
iteration support:

container.__iter__()

Return an iterator object. The object is required
to support the iterator protocol described below. If a container
supports different types of iteration, additional methods can be
provided to specifically request iterators for those iteration types.
(An example of an object supporting multiple forms of iteration would
be a tree structure which supports both breadth-first and depth-first
traversal.) This method corresponds to the tp_iter slot of the type
structure for Python objects in the Python/C API.

The iterator objects themselves are required to support the following
two methods, which together form the iterator protocol:

iterator.__iter__()

Return the iterator object itself. This is
required to allow both containers and iterators to be used with the
for and in statements. This method corresponds to the tp_iter slot of
the type structure for Python objects in the Python/C API.

iterator.next()

Return the next item from the container. If there are
no further items, raise the StopIteration exception. This method
corresponds to the tp_iternext slot of the type structure for Python
objects in the Python/C API.

但是…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
>>> dir('aa')
['__add__', '__class__', '__contains__', '__delattr__', '__doc__',
 '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__',
 '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__',
 '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__',
 '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__',
 '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
 '_formatter_field_name_split', '_formatter_parser', 'capitalize',
 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs',
 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower',
 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip',
 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition',
 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip',
 'swapcase', 'title', 'translate', 'upper', 'zfill']

这里我看不到任何uuIter_uuu()或next()。那它为什么会起作用呢?


迭代器在python 2.2中是新的。旧的方法是序列协议(使用基于0的索引实现__getitem__),但仍然有效。