[Python-ideas] PEP 380 alternative: A yielding function (original) (raw)
Guido van Rossum guido at python.org
Wed Jul 28 16:26:25 CEST 2010
- Previous message: [Python-ideas] PEP 380 alternative: A yielding function
- Next message: [Python-ideas] PEP 380 alternative: A yielding function
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Tue, Jul 27, 2010 at 9:38 PM, Carl M. Johnson <cmjohnson.mailinglist at gmail.com> wrote:
What would this code do:
def yieldiftrue(x): if x: yield(x) def maybeyield(x): if calculateproperty(x): yieldiftrue(x) else: return None maybeyield(5) When maybeyield(5) is called different things need to happen depending on whether it’s a function or a generator. If it’s a generator, it shouldn’t execute calculateproperty(x) yet, because generators don’t execute their contents until someone says next(maybeyield(5)) (or maybeyield(5).next() in Py2.x). On the other hand, if it’s not a generator but a function, then it should run calculateproperty right away. You could try starting out as a function and then switching to being a generator if anything that you call has a call to yield inside of it, but that strikes me as extremely clumsy and complicated, and it could lead to unpleasant surprises if calculateproperty has side-effects. ISTM there’s no way to do something like PEP-380 without requiring that some special keyword is used to indicate that the def is creating a generator and not creating a function. There other directions to take this besides yield from (for example, we could replace def with gen or some such and give return a special meaning inside a gen statement), but to try to do it without any kind of keyword at the site of the caller means violating "In the face of ambiguity, refuse the temptation to guess.”
Well, in a statically typed language the compiler could figure it out based on the type of the things being called -- "generator-ness" could be propagated by the type system just like "thows a certain expression" is in Java.
-- --Guido van Rossum (python.org/~guido)
- Previous message: [Python-ideas] PEP 380 alternative: A yielding function
- Next message: [Python-ideas] PEP 380 alternative: A yielding function
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]