如何管理 Python 循环中的副作用?

How to manage side effects in Python loops?

在我的许多项目中,我遇到了以下 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 创建一个包装器:

def printer(gen):
    for idx, item in gen:
        if idx < 10:
            print(item)
        elif idx == 10:
            print('...')
        yield idx, item
    if idx >= 10:
        print(item)

并像这样使用:

for idx, item in printer(enumerate(items)):
    # apply and accumulate
    state = f(state, item)