问题描述
当字符串没有 __iter__ 函数时,我们如何迭代 python 字符串?
How is it that we can iterate over python strings when strings don't have an __iter__ function?
$ 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>
>>>
更重要的是(无论如何迭代字符串),为什么 python 字符串不遵循与其他所有内容相同的约定.特别是当 Python 文档说需要 __iter__ 函数时 http://docs.python.org/library/functions.html#iter ?
And more importantly (however strings are iterated over), why do python strings not follow the same convention as everything else. Particularly when the Python docs say that the __iter__ function is needed http://docs.python.org/library/functions.html#iter ?
推荐答案
来自您的链接:
或者它必须支持序列协议(__getitem__() 方法整数参数从 0 开始).
or it must support the sequence protocol (the
__getitem__()method with integer arguments starting at 0).
In [1]: 'foo'.__getitem__(0)
Out[1]: 'f'
这篇关于为什么 python 字符串没有 __iter__ 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)