相关疑难解决方法(0)

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

当字符串没有__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

python string iteration

20
推荐指数
1
解决办法
2595
查看次数

标签 统计

iteration ×1

python ×1

string ×1