为什么python字符串没有__iter__函数?

Ale*_*ird 20 python string iteration

当字符串没有__iter__函数时,我们如何迭代python字符串?

$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> "asdf".__iter__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute '__iter__'
>>> it = iter("asdf")      
>>> it
<iterator object at 0xb736f5ac>
>>> 
Run Code Online (Sandbox Code Playgroud)

更重要的是(但是字符串被迭代),为什么python字符串不遵循与其他所有相同的约定.特别是当Python文档说__iter__需要函数http://docs.python.org/library/functions.html#iter

nmi*_*els 38

从您的链接:

或者它必须支持序列协议(__getitem__()整数参数从0开始的方法).

In [1]: 'foo'.__getitem__(0)
Out[1]: 'f'
Run Code Online (Sandbox Code Playgroud)