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

2024-06-02 04:38:38 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的许多项目中,我遇到了以下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__上完成时执行操作,因此此时仍然可以更新其状态

我遇到的问题是,它为使用这种上下文管理器的每个循环添加了一个缩进,并且上下文管理器没有绑定到循环。你有更好的办法解决这个问题吗?你知道吗


Tags: andinlogforstatusdisplayitemsitem
1条回答
网友
1楼 · 发布于 2024-06-02 04:38:38

您可以为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)

相关问题 更多 >