blueFast
blueFast

Reputation: 44331

Reyield a generator

I would do this:

def walk(samples):
    for d in range(samples):
        yield d

def walk200():
    for d in walk(200):
        yield d

But actually what I want is this, to make the code shorter:

def walk200():
    reyield walk(200)

How do I do reyield?

Upvotes: 2

Views: 161

Answers (2)

Alex Hall
Alex Hall

Reputation: 36013

In your specific example, you can simply return walk(200), and that will work in all python versions. yield from is only necessary in certain cases.

Upvotes: 2

Phydeaux
Phydeaux

Reputation: 2855

Python 3.3 and up:

def walk200():
    yield from walk(200)

For lower versions, you are stuck with the code you posted.

Upvotes: 6

Related Questions