在我的许多项目中,我遇到了以下Python循环模式:
for idx, item in enumerate(items):
# apply and accumulate
state = f(state, item)
# display status/log something depending on state/item/iteration
side_effect(idx, state, item)
如果我想把副作用的处理抽象化,那就复杂了。例如,我想打印前10项,然后只打印点,最后打印最后一项:
for idx, item in enumerate(items):
# apply and accumulate
state = f(state, item)
# display status/log something depending on state/item/iteration
if idx < 10:
print(item)
elif idx == 10:
print('...')
if idx >= 10:
print(item)
# in general, we cannot assume how many items there are, but
# it's a bit ugly imo because item is out of scope here
# if we solve this by keeping a reference to last item, it's worse imo
假设我想让这种行为对许多循环通用。为此,我在循环周围使用了一个上下文管理器,也在循环内部调用它来处理副作用,如下所示:
with Printer(...) as printer:
for idx, item in enumerate(items):
# apply and accumulate
state = f(state, item)
# display status/log something depending on state/item/iteration
printer.do_your_thang(item)
打印机跟踪迭代,甚至可以在循环在__exit__
上完成时执行操作,因此此时仍然可以更新其状态
我遇到的问题是,它为使用这种上下文管理器的每个循环添加了一个缩进,并且上下文管理器没有绑定到循环。你有更好的办法解决这个问题吗?你知道吗
您可以为
enumerate
创建包装器:使用如下:
相关问题 更多 >
编程相关推荐