[Python-ideas] return from (was Re: Tail recursion elimination)

Oscar Benjamin oscar.j.benjamin at gmail.com
Tue Jan 21 11:36:19 CET 2014


On Tue, Jan 21, 2014 at 07:46:12PM +1300, Greg Ewing wrote:
> Jonathan Slenders wrote:
> 
> >    @coroutine
> >    def a():
> >        return (yield from b())
> >
> >You could write it as:
> >
> >    def a():
> >        return b()
> 
> I'm guessing you mean
> 
>    def a():
>       return from b()
> 
> but that wouldn't be a coroutine, because it doesn't
> contain a 'yield' anywhere.

If b() is a generator/iterator then the second example removes the frame
associated fom a() from the stack while you iterate:

for x in a():
    # one less frame on the stack at this point


Oscar


More information about the Python-ideas mailing list