4

I am wondering, after reading about generators in Improve Your Python: 'yield' and Generators Explained, but not having experimented with them yet, why the syntactic notation for a generator function is not different from that of a regular function. The most obvious choice, in my thought experiment, would be:

generator generator_name(param):
    # ...
    yield some_other_value

Instead of:

def generator_name(param):
    # ...
    yield some_value

Now, when one is reading Python code, it seems that one needs to search for the word 'yield' first before understanding that some function is a generator function. Or, is there a Python convention that demands generator functions have a indicative name? like generate_some_value_from_y?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Michahell
  • 4,905
  • 5
  • 29
  • 45
  • 1
    Because functions that can be treated as generator functions aren't fundamentally different from normal functions. You can use them in all the places that you can use non-generator functions. – yan Nov 24 '13 at 23:23
  • 2
    Because a generator function is not itself a generator, it *produces* a generator. – Martijn Pieters Nov 24 '13 at 23:24
  • @Martijn Pieters yes, entirely clear, can you Answer this? than i can mark it resolved. Also i just found out about that when reading this S.O. post: http://stackoverflow.com/questions/231767/the-python-yield-keyword-explained/231855#231855 – Michahell Nov 24 '13 at 23:30

1 Answers1

7

Quoting from PEP 255, the proposal that introduced generators to Python, where Guido van Rossum (the Benevolent Dictator for Life, BDFL) explains why there is no separate keyword:

Issue: Introduce another new keyword (say, gen or generator) in place of def, or otherwise alter the syntax, to distinguish generator-functions from non-generator functions.

Con: In practice (how you think about them), generators are functions, but with the twist that they're resumable. The mechanics of how they're set up is a comparatively minor technical issue, and introducing a new keyword would unhelpfully overemphasize the mechanics of how generators get started (a vital but tiny part of a generator's life).

Pro: In reality (how you think about them), generator-functions are actually factory functions that produce generator-iterators as if by magic. In this respect they're radically different from non-generator functions, acting more like a constructor than a function, so reusing def is at best confusing. A yield statement buried in the body is not enough warning that the semantics are so different.

BDFL: def it stays. No argument on either side is totally convincing, so I have consulted my language designer's intuition. It tells me that the syntax proposed in the PEP is exactly right - not too hot, not too cold. But, like the Oracle at Delphi in Greek mythology, it doesn't tell me why, so I don't have a rebuttal for the arguments against the PEP syntax. The best I can come up with (apart from agreeing with the rebuttals ... already made) is "FUD". If this had been part of the language from day one, I very much doubt it would have made Andrew Kuchling's "Python Warts" page.

In essence, a generator function produces a generator, it is not a generator itself.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343