Equivalent of `return` for Python generators

Eli Rose -- REINSTATE MONICA

Sometimes, when rewriting recursive functions as generators, I miss the brevity of return.

"""
Returns a list of all length n strings that can be made out of a's and/or b's.
"""
def ab_star(n):
    if n == 0:
        return [""]

    results = []
    for s in ab_star(n - 1):
        results.append("a" + s)
        results.append("b" + s)

    return results

turns into

"""
Generator for all length n strings that can be made out of a's and/or b's.
"""
def ab_star(n):
    if n == 0:
        yield ""

    else:
        for s in ab_star(n - 1):
            yield "a" + s
            yield "b" + s

It's that else that bugs me. I wish there was a way to say "yield, and this is it, so exit the function". Is there a way?

shx2

Don't miss return, use it.

You can return right after you yield.

def ab_star(n):
    if n == 0:
        yield ""
        return
    for s in ab_star(n - 1):
        yield "a" + s
        yield "b" + s

An alternative is to use return in both cases, where the first case returns a sequence of length 1, and the second returns a generator-expression:

def ab_star(n):
    if n == 0:
        return ( "", )
    return ( c+s for s in ab_star(n - 1) for c in 'ab' )

This avoidance of yield avoids the limitation that you cannot use both return <value> and yield in the same function.

(This works in your case because your function doesn't have to be a generator. Since you only iterate over the results, it can also return a tuple.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Equivalent of `return` for Python generators

From Dev

OCaml equivalent of Python generators

From Dev

Is there a straightforward lisp equivalent of Python's generators?

From Dev

Function generators vs class generators in Python 3

From Dev

Generators and for loops in Python

From Dev

python generator of generators?

From Dev

DFS algorithm in Python with generators

From Dev

Python : Behaviour of send() in generators

From Dev

Unexpected output in Python Generators

From Dev

Redundant use of generators? (Python)

From Dev

Trouble understanding python generators

From Dev

python generators for concurrency

From Dev

Is there a `let` or `as` keyword for Python generators?

From Dev

Iterating Generators in a For Loop in Python

From Dev

Argument checking in Python generators

From Dev

Python infinite recursion with generators

From Dev

Python Loop Behavior with Generators

From Dev

python generators garbage collection

From Dev

Python generators - float( ( yield ) )?

From Dev

Genshi and Python Generators (yield)

From Dev

Python: Create Generators at Runtime

From Dev

Generators in python not working as expected

From Dev

Is there a `let` or `as` keyword for Python generators?

From Dev

Python and Number Generators

From Dev

Python generators and reduce

From Dev

python StopIteration with Generators and lists

From Dev

How to modify the return address in Python 2 (or achieve an equivalent result)

From Dev

Resolution of yield return value in JavaScript generators

From Dev

Diff between "return" and "yield" stmts in generators