[Python-ideas] keyword for introducing generators
Chris Angelico
rosuav at gmail.com
Sat Aug 16 23:55:36 CEST 2014
On Sun, Aug 17, 2014 at 7:46 AM, Neil Girdhar <mistersheik at gmail.com> wrote:
> I'm sure this has been suggested before, but I just spent two days trying to
> figure out why a method wasn't being called only to find that I'd
> accidentally pasted a yield into the function. What is the argument against
> a different keyword for introducing generator functions/methods?
There are quite a few changes to a function based on its body, like
how the presence of assignment causes a name to be local unless
explicitly declared otherwise. It's not necessary to predeclare
everything.
But if you're having trouble with a function like that, maybe a little
decorator would help:
def announce(f):
def inner(*a,**kw):
print("Calling:",f.__name__)
ret=f(*a,**kw)
print("Return value is a",type(ret))
return ret
return inner
Decorate a function @announce, and it'll tell you (a) that it's being
called, and (b) what type its return value is. If that's a generator,
well, there's your answer.
ChrisA
More information about the Python-ideas
mailing list