【发布时间】:2019-10-27 12:32:09
【问题描述】:
我正在尝试在 python 的 itertools 中模拟“链”函数。
我想出了以下生成器。
# Chain make an iterator that returns elements from the first iterable
# until it is exhausted, then proceeds to the next iterable, until all
# of the iterables are exhausted.
def chain_for(*a) :
if a :
for i in a :
for j in i :
yield j
else :
pass
如何在一个类中模拟相同的功能? 由于函数的输入是任意数量的列表,我不确定是否可以在类中使用打包/解包,如果可以,我不确定如何在 'init' 方法中解包.
class chain_for :
def __init__(self, ...) :
....
def __iter__(self) :
self
def __next__(self) :
.....
谢谢。
【问题讨论】:
-
“我不确定打包/解包在使用类时是否有用”我不确定使用打包/解包的位置如何影响其有用性
-
我的意思是它是否可以在课堂上使用。更新了问题。
-
是的,它可以....
-
怎么样?有什么建议或参考吗?
-
def chain_for(*a):和def __init__(self, *a):之间没有(太大)区别
标签: python class itertools chain