2

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?

blueFast
  • 41,341
  • 63
  • 198
  • 344

2 Answers2

6

Python 3.3 and up:

def walk200():
    yield from walk(200)

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

Phydeaux
  • 2,795
  • 3
  • 17
  • 35
2

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.

Alex Hall
  • 34,833
  • 5
  • 57
  • 89
  • Interesting. Can you clarify? When and why is it necessary, and when not? – blueFast Sep 14 '17 at 10:30
  • @dangonfast (1) if you have several yield statements in one function and you want to delegate only some of them to a different function, or more importantly (2) if you're using a generator as a coroutine, see https://stackoverflow.com/questions/9708902/in-practice-what-are-the-main-uses-for-the-new-yield-from-syntax-in-python-3 – Alex Hall Sep 14 '17 at 10:34