简体   繁体   中英

How can I do a conditional yield with Python?

I have to refactor some code. The original code was something like:

 while (yield _requires_payment(state)):
        did_pass_limit = yield _did_pass_limit(state)
        if not did_pass_limit:
            if existing_count is None:
                yield send_info_log("some stuff")
            yield send_info_log(f"more stuff")

So I refactored to:

    if yield _requires_payment(state):
        yield send_info_log(f"stuff")

If it matters, the function definition is:

@dialog(version="1.0")
async def _requires_payment(state):
    return await apply_payment_status_check(state, check_types=["window"])

But this creates an issue. What am I doing wrong?

If you want to use yield as an expression there, surround it with parentheses . Just like it was in your while condition.

(In assignment statements , like x = yield y , it's not needed, because that directly uses yield_expression instead of yield_atom .)

if evaluates Boolean values Try:


 if (yield _requires_payment(state)):
        yield send_info_log(f"stuff")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM