Recursive generator in Python 3.5

Gregory Ewing greg.ewing at canterbury.ac.nz
Mon Oct 31 18:25:33 EDT 2016


tpqnnd01 at gmail.com wrote:

> def fg(args):
>   if not args:
>     yield ""
>     return
>   for i in args[0]:
>     for tmp in fg(args[1:]):
>       yield i + tmp
>   return

The final return is redundant, since there is an implicit return
at the end, just like an ordinary function. The other one is just
performing an early exit from the iteration. You could write it
without any returns like this:

def fg(args):
   if not args:
     yield ""
   else:
     for i in args[0]:
       for tmp in fg(args[1:]):
         yield i + tmp

> this is the Tower of Hanoi with  recursive generator but it do
 > not have the 'return' here

-- 
Greg



More information about the Python-list mailing list