1

Simple method with just return keyword returns a None

def abc():
    return
print(abc())

Output: None

Similarly,

def abc():
    return None
print(abc())

Output: None

However if we use this in generator

def abc():
    yield 1
    return None
print(abc())

it gives

SyntaxError: 'return' with argument inside generator

where as

def abc():
    yield 1
    return
print(abc())

gives

<generator object abc at 0x7f97d7052b40>

Why do we have this difference in behavior?

sophros
  • 14,672
  • 11
  • 46
  • 75
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
  • 5
    It clearly states that return with an argument is a syntax error. The grammar does not check or care whether the argument is None, the SyntaxError already happens before. – Jan Christoph Terasa Feb 04 '20 at 13:09
  • 1
    @JanChristophTerasa - That is pretty much what the OP is asking, why is there a difference in the behaviour – Sayse Feb 04 '20 at 13:10
  • 5
    You appear to be using an old version of Python. – chepner Feb 04 '20 at 13:11
  • Does this answer your question? [Why can't I use yield with return?](https://stackoverflow.com/questions/18215828/why-cant-i-use-yield-with-return) – zamir Feb 04 '20 at 13:12
  • 1
    [This answer](https://stackoverflow.com/a/15809390/6699447) is worth of reading – Abdul Niyas P M Feb 04 '20 at 13:13
  • This hasn't been an error since [Python 3.3](https://www.python.org/dev/peps/pep-0380/), about *ten years ago*! – MisterMiyagi Feb 04 '20 at 13:20
  • By the way return value of generator does not reach to us. We are using `return` in generators for terminating them. – Ekrem Dinçel Feb 04 '20 at 13:52

1 Answers1

2

A bare return is useful to break out early from a generator.

Meanwhile return None is just a special case of return <a value>, and before yield from (PEP 380) there was no support or use case for returning a value from a generator. So it was forbidden in order to leave the design space open: by forbidding returning values in generators, Python's designers made it possible to allow it later with new semantics as that would not break existing code.

Had they allowed a return value without doing anything with it, there was a risk userland code would break. That's why from a forward-compatibility perspective it's often better to restrict APIs as much as possible, everything you leave open users will take advantage of, and it becomes risky to change.

Masklinn
  • 34,759
  • 3
  • 38
  • 57
  • Thanks. Now that it is supported can you add how returning with yield is used to overcome issues you have stated with previous design? – Aniket Thakur Feb 12 '20 at 08:29
  • I don't understand your question. There were no issues in the previous design, it just didn't have any use for returning values from generators so forbid it. Then PEP 380 added a use case for it, and thus allowed it. There is no overcoming, originally forbidding the form allowed enabling it later in a safe and backwards-compatible manner. – Masklinn Feb 12 '20 at 09:35