[Python-ideas] Inline Functions - idea

Ron Adam ron3200 at gmail.com
Thu Feb 6 21:09:34 CET 2014



On 02/06/2014 05:14 AM, Nick Coghlan wrote:
> As far as what you're proposing goes, is it essentially a way to
> declare a function like (spelling out the lambdas fully):
>
>      def S(x):
>          def _second(y):
>              def _third(z):
>                  return x(z)(y(z))
>              return _third
>          return _second
>
> As something much shorter like this:
>
>      def S (x)(y)(z):
>          return x(z)(y(z))


The examples just had all the inner calls at the top.  I don't see a 
requirement for that.  By being able to place the capture/continuation 
statements anywhere, it opens up a lot of other (and new) possibilities.

> The main potential benefit I could see to a construct like that is
> that it may allow the more consistent creation of closures that
> support pickling, since the outer functions are guaranteed not to have
> any side effects and to have argument capture as their*only*
> significant state. This means that you could take the inner function,
> pickle it along with its closure variables and reconstruct that at the
> far end, only relying on the name of the outer function.

Would the calls need to be at the top for that?


Another use is as auto_start generators that don't need a next() call to start.

        def continue auto_gen:
            # do pre-setup stuff.
            ...
            :from(a, b, c)    # get initial values.
            # rest of generator with normal yields.
            ...


Or maybe something like...

       def continue select:
           :from(pick_mode)
           if (mode == mode_a):
               :from(cmd, *args)
               ... do stuff
               return cmd(*args)
           else:
               :from(cmd, a, b, c)
               ... do other stuff
               return cmd(a, b, c)

I think the ability to put them where they are needed is an important part 
of the suggestions.

Cheers,
    Ron



More information about the Python-ideas mailing list