string:__iter __()? [英] string: __iter__()?

查看:55
本文介绍了string:__iter __()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!


为了好奇,我想知道为什么字符串不支持

迭代协议!?有更深层次的原因吗?


>> hasattr(''SomeString' ',''__ item__'')



False


在Python 2.5中它'实际上很容易获得一个:


>> myIter =(c for c在''SomeString'')
myIter.next()



''S''


感谢您的信息!


Chris

解决方案

mrquantum写道:


为了好奇,我想知道为什么字符串不支持

迭代协议!?



真的吗? iter(SomeString)对我来说效果很好。


< / F>



" mrquantum" < mr ******* @ holon.atwrites:


只是出于好奇我想知道为什么字符串不支持

迭代协议!?有更深层次的原因吗?


> hasattr(''SomeString'',''__ item__'')



False



这有点奇怪。但至少在2.3.4中:


Python 2.3.4(#1,2005年2月2日,12:11:53)

[GCC 3.4。 2 20041017(Red Hat 3.4.2-6.fc3)] on linux2

输入help,copyright,credit等等。或许可证或欲获得更多信息。


>> a = iter(''hi there'')
a



< iterator object at 0xb7c7f8ec>


>> print [x for x in a]



[''h'',''我','''','t'',''h'','' e'',''r'',''e'']


> >>



所以它看起来仍然存在,但来自一些较旧的

层实施。


Am Wed,2006年10月4日12:03:41 +0200 schrieb Fredrik Lundh:


>

真的吗? iter(SomeString)对我来说效果很好。



嗯,对吧!


但是为什么呢? 'dir(''SomeString'')显示__iter__方法?似乎

的表现方式不同于例如名单?知道原因吗?


Hello!

Just for curiosity i''d like to know why strings don''t support the
iteration protocoll!? Is there some deeper reason for this?

>>hasattr(''SomeString'', ''__iter__'')

False

In Python 2.5 it''s actually simple to obtain one:

>>myIter = (c for c in ''SomeString'')
myIter.next()

''S''

Thanks for info!

Chris

解决方案

mrquantum wrote:

Just for curiosity i''d like to know why strings don''t support the
iteration protocoll!?

really? iter("SomeString") works just fine for me.

</F>



"mrquantum" <mr*******@holon.atwrites:

Just for curiosity i''d like to know why strings don''t support the
iteration protocoll!? Is there some deeper reason for this?

>hasattr(''SomeString'', ''__iter__'')

False

It is a little but odd. But at least in 2.3.4:

Python 2.3.4 (#1, Feb 2 2005, 12:11:53)
[GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>a = iter(''hi there'')
a

<iterator object at 0xb7c7f8ec>

>>print [x for x in a]

[''h'', ''i'', '' '', ''t'', ''h'', ''e'', ''r'', ''e'']

>>>

So it looks like the iter is still there, but comes from some older
layer of the implementation.


Am Wed, 04 Oct 2006 12:03:41 +0200 schrieb Fredrik Lundh:

>
really? iter("SomeString") works just fine for me.

Hmm, right!

But then why doesn''t dir(''SomeString'') show an __iter__ method? Seems to
be implmented differently than with e.g. lists? Know why?


这篇关于string:__iter __()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆