[Python-ideas] keyword for introducing generators

Neil Girdhar mistersheik at gmail.com
Sun Aug 17 00:04:45 CEST 2014


This is only a good solution once I realize that the method isn't being
called after which, I now know what to look for.  The whole point is to
defensively design a language so that I don't get into this problem in the
first place.  Since I always know when I'm writing a function whether I
want it to be automatically generator-returning using the yield keyword or
not, then why not let me specify that?  That way if the function gets long
and I forget yield or accidentally include it, I get a reasonable error.

You're right that it's not necessary to predeclare everything, but a
different keyword is hardly more work.  (And with respect to local,
nonlocal and global variables, we often do predeclare those.)

Best,
Neil


On Sat, Aug 16, 2014 at 5:55 PM, Chris Angelico <rosuav at gmail.com> wrote:

> 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
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
> --
>
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "python-ideas" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/python-ideas/5-Qm2od4xQ8/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> python-ideas+unsubscribe at googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140816/6163c44d/attachment-0001.html>


More information about the Python-ideas mailing list