更多婴儿吱吱声 - 课堂上的迭代器 [英] More baby squeaking - iterators in a class

查看:44
本文介绍了更多婴儿吱吱声 - 课堂上的迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everyone先生,


来自: http://docs.python.org/tut/node11.ht...00000000000000


"定义__iter __( )使用next()

方法返回对象的方法。如果类定义了next(),那么__iter __()就可以返回

self:"


事情是,我试图定义__iter __()直接没有明确定义接下来的b $ b(毕竟,这篇文章的结论应该是

是可能的)。

class R :

def __init __(self,d):

self.d = d

self.i = len(d)

def __iter __(self):

如果self.i == 0:

提高StopIteration

self.i - = 1

返回self.d [self.i]

s = R(''垃圾邮件'')
dir(s)
[''__doc__'',''_ _ _ _ _ _'''''''_ _ _ _ _ _''',''_ _ _ _ _ _''''''''''''''' ',''我'']


显然不,没有next()方法。如果迭代器有效,让我们看看



s .__ iter __()
''m's .__ iter __()
'' a's .__ iter __()
''p''s .__ iter __()
's's .__ iter __()
Traceback(最近一次调用最后一次):

文件"<互动输入>",第1行,在?

文件"<交互式输入>",第7行,在__iter__

StopIteration


好​​的,这部分有效。但是这个:

s = R(''spam'')
我在s:



打印i $ / b

回溯(最近一次调用最后一次):

文件"<互动输入>",第1行,在?

TypeError:__iter__返回类型''str'的非迭代器


那么它是什么? next()方法是否要明确定义

?这就是维基百科所说的:

http: //en.wikipedia.org/wiki/Iterator#Python


"任何用户定义的类都可以支持标准迭代(

隐式或显式)通过定义__iter __()方法创建

迭代器对象。然后迭代器对象需要定义一个

__iter __()方法以及next()方法。



-

这是人类在Python编程协会中的生活。

解决方案

" Bulba!" < BU *** @ bulba.com>在消息中写道

news:nj ******************************** @ 4ax.com ...

Hello Everyone先生,

来自: http://docs.python.org/tut/node11.ht...00000000000000

"定义__iter __( )使用next()
方法返回对象的方法。如果类定义了next(),则__iter __()可以返回
self:"

事情是,我试图直接定义__iter __()而没有明确定义
接下来(毕竟,这段经文的结论应该是可能的)。


我根本没有从引用的段落中得到它,尽管它有点不透明。它表示你的__iter __()方法必须*返回一个带有

next()方法的对象*;你的__iter __()方法下面没有返回这样的对象,

而是返回一个字符串。然后它说*如果*你的班级定义了

next(),你的没有,__ __()可以返回自己。


[空格插入;你应该注意到许多新闻阅读器剥离了TAB

字符...]

class R:
def __init __(self,d):
self .d = d
self.i = len(d)
def __iter __(self):
如果self.i == 0:
提高StopIteration
自我。我 - = 1
返回self.d [self.i]




解决方案:替换__ iter __用下一个在上面的类定义中,

然后加到最后:

def __iter __(self):

返回self

-

我实际上并没有阅读我的hotmail帐户,但如果你真的想联系我,你可以用

来代替hotmail。


Bulba! < BU *** @ bulba.com>写道:

那是什么? next()方法是否明确定义?




必须定义,无论是否明确(例如通过生成器)。

Alex




" Bulba!" < BU *** @ bulba.com>在消息中写道

news:nj ******************************** @ 4ax.com ...

"定义__iter __()方法,该方法返回带有next()
方法的对象。如果类定义了next(),则__iter __()可以返回
self:"

事情是,我试图直接定义__iter __()而没有明确定义
接下来(毕竟,这段经文的结论应该是可能的)。


它是,见下文。

类R:
def __init __(self,d):
self.d = d
self.i = len(d)
def __iter __(self):
如果self.i == 0:
提高StopIteration
self.i - = 1
返回self.d [self.i]




将''return''改为''yield''。然后r .__ iter __()*将*根据需要使用.next方法返回一个

(生成器)迭代器。但是在结束之前它只会产生一个值。由于__iter__只需要调用一次,因此需要在生成器中显式循环。对于

实例


def __iter __(自我):

i,d = self.i,self.d

而我

i = - 1

收益率d [i]


复制i使得生成器不具有指责性。


(PS,在发布的代码中使用空格而不是标签。)


Terry J. Reedy


Hello Mr Everyone,

From:
http://docs.python.org/tut/node11.ht...00000000000000

"Define a __iter__() method which returns an object with a next()
method. If the class defines next(), then __iter__() can just return
self:"

The thing is, I tried to define __iter__() directly without explicit
defining next (after all, the conclusion from this passage should
be that it''s possible).
class R:
def __init__(self, d):
self.d=d
self.i=len(d)
def __iter__(self):
if self.i == 0:
raise StopIteration
self.i -= 1
return self.d[self.i]

s=R(''spam'') dir(s) [''__doc__'', ''__init__'', ''__iter__'', ''__module__'', ''d'', ''i'']

Apparently no, there is no next() method. Let''s see
if iterator works:
s.__iter__() ''m'' s.__iter__() ''a'' s.__iter__() ''p'' s.__iter__() ''s'' s.__iter__() Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "<interactive input>", line 7, in __iter__
StopIteration

OK, this part works. But this:
s=R(''spam'')
for i in s:


print i

Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: __iter__ returned non-iterator of type ''str''

So which is it? Does next() method HAS to be defined
explicitly? That''s what Wikipedia says:

http://en.wikipedia.org/wiki/Iterator#Python

"Any user defined class can support standard iteration (either
implicit or explicit) by defining an __iter__() method which creates
an iterator object. The iterator object then needs to define both an
__iter__() method as well as a next() method."


--
It''s a man''s life in a Python Programming Association.

解决方案

"Bulba!" <bu***@bulba.com> wrote in message
news:nj********************************@4ax.com...

Hello Mr Everyone,

From:
http://docs.python.org/tut/node11.ht...00000000000000

"Define a __iter__() method which returns an object with a next()
method. If the class defines next(), then __iter__() can just return
self:"

The thing is, I tried to define __iter__() directly without explicit
defining next (after all, the conclusion from this passage should
be that it''s possible).
I don''t get that from the passage quoted, at all, although it is somewhat
opaque. It says that your __iter__() method must *return an object* with a
next() method; your __iter__() method below doesn''t return such an object,
but instead returns a string. It then says that *if* your class defines
next(), which yours doesn''t, __iter__() can return self.

[spaces inserted; you should note that many newsreaders strip the TAB
character...]
class R:
def __init__(self, d):
self.d=d
self.i=len(d)
def __iter__(self):
if self.i == 0:
raise StopIteration
self.i -= 1
return self.d[self.i]



Solution: replace "__iter__" with "next" in the class definition above,
then add to the end:

def __iter__(self):
return self
--
I don''t actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.


Bulba! <bu***@bulba.com> wrote:

So which is it? Does next() method HAS to be defined
explicitly?



It has to be defined, whether explicitly or not (e.g. via a generator).
Alex



"Bulba!" <bu***@bulba.com> wrote in message
news:nj********************************@4ax.com...

"Define a __iter__() method which returns an object with a next()
method. If the class defines next(), then __iter__() can just return
self:"

The thing is, I tried to define __iter__() directly without explicit
defining next (after all, the conclusion from this passage should
be that it''s possible).
It is, see below.
class R:
def __init__(self, d):
self.d=d
self.i=len(d)
def __iter__(self):
if self.i == 0:
raise StopIteration
self.i -= 1
return self.d[self.i]



Change ''return'' to ''yield''. Then r.__iter__() *will* return a
(generator)iterator with a .next method, as required. But it will only
yield one value before running off the end. Since __iter__ is meant to be
called only once, you need to loop explicitly in the generator. For
instance

def __iter__(self):
i,d = self.i, self.d
while i
i =- 1
yield d[i]

Copying i makes the generator non-destuctive.

(PS, use spaces, not tabs, in posted code.)

Terry J. Reedy


这篇关于更多婴儿吱吱声 - 课堂上的迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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