Python 调度字典(作业)
p、 本规范的结果应为:Python 调度字典(作业),python,eclipse,python-3.x,Python,Eclipse,Python 3.x,p、 本规范的结果应为: def get_map_iterator(slist,gfunc=None): index = 0 def Next(): nonlocal index x = slist[index] index = index + 1 return x def has_more(): if slist[index] != None : return True else: return False d
def get_map_iterator(slist,gfunc=None):
index = 0
def Next():
nonlocal index
x = slist[index]
index = index + 1
return x
def has_more():
if slist[index] != None :
return True
else:
return False
dispatch = {
'Next': lambda: gfunc(Next()),
'has_more': has_more
}
return dispatch
it = get_map_iterator((1,3,6))
for i in range(i,6):
it['Next']()
it = get_map_iterator((1,3,6),lambda x:1/x)
while it['has_more']():
it['next']()
对gfunc的更改将如何影响这一点,我的意思是,如果我确实获得了func或我没有获得func,我需要更改什么才能使其工作。get\u map\u iterator()
返回函数对象(dispatch
)。您试图将该对象视为字典
您想将其命名为:
1
3
6
no more items
no more items
1.0
0.33333
0.166666
您的dispatch()
函数本身不会返回另一个函数对象,因此您不会调用it()
返回的任何函数
您的有\u更多的路由,然后失败:
while it('has_more'):
it('Next')
您可以像最初一样使用此返回值,通过键查找可调用项,然后调用它。是的,我注意到,谢谢,还有一个问题。”TypeError:'bool'对象不可调用'@Mike.G:删除()
,最后,您将不会返回函数。TypeError:'dict'对象不可调用-现在此错误已随您告诉我的更改而消失do@Mike.G:我说过你必须回到你原来的代码,所以使用it['has_more']()
和it['Next']()
。当我使用时,我得到的bool是不可调用的。
>>> it('has_more')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 17, in dispatch
File "<stdin>", line 9, in has_more
TypeError: next expected at least 1 arguments, got 0
dispatch = {
'Next': lambda: gfunc(Next()),
'has_more': has_more
}
return dispatch