【发布时间】:2016-12-30 19:56:48
【问题描述】:
我有一个初始课程:
class foo:
def __init__(self, a, b):
self.a = a
self.b = b
还有另一个使用 foo 类的类:
class bar:
def __init__(self, foos):
self.foos = sorted(foos, key=attrgetter('a'))
其中foos 是foo 的列表。我现在想要列出foo 的列表,看起来像:
lofoos = [[foo1, foo2, foo3], [foo4, foo5, foo6] ...]
我想使用地图功能来做到这一点:
list(map(lambda foos: bar(foos), lofoos))
但这会返回错误:
TypeError: iter() returned non-iterator of type 'foo'.
有没有简单的解决方案?
【问题讨论】:
-
请给minimal reproducible example提供完整的回溯。
-
很简单:
foo不是迭代器。 -
好的,有没有办法让 bar 成为迭代器?
-
它对我有用...
-
您的代码库中似乎有一个损坏的
__iter__实现。
标签: python python-3.x class