[Python-ideas] Inline Functions - idea

Ron Adam ron3200 at gmail.com
Thu Feb 6 11:34:16 CET 2014



On 02/05/2014 08:49 AM, Skip Montanaro wrote:
> I'm not convinced this particular example needs an inline function.
> Just write saveLine() as a normal function and pass in your locals:
>
> def f():
>      a = 1
>      b = 7
>      compute(**locals())
>
> def g():
>      a = 3
>      b = 0.9
>      compute(**locals())
>
> def compute(**args):
>      print args["a"] + args["b"]
>
> f()
> g()
>
> That's not to say inline functions might not be handy. It's possible
> that you can implement them without modifying the language either. A
> peephole optimizer could inline small functions which are used a lot.
>
> Skip

The other day I was trying to figure out how to do lamba calculus without 
using closures (or other tricks).

 >>> def I (x): return x
 >>> def K (x):
...     return lambda y: x
 >>> def S (x):
...    return lambda y: lambda z: x(z)(y(z))
 >>> I("A")
'A'
 >>> K("A")("B")
'A'
 >>> S(K)(K)("A")
'A'

To do it without closures isn't easy.  But it did give me an interesting 
idea that could simplify a lot of problems.

Basically...
      # S := λx.λy.λz.x z (y z)

      def continue S:
         :from(x)
         :from(y)
         :from(z)
         return x(z)(y(z))

  And it would be used exactly like the lambda example above.

     S(K)(K)("A")  --> 'A'

That's equation is supposed to give the same result back out..
   (* That is if I have it right?)

Each call returns the updated frame but we only have one code object which 
makes it easier to write and think about.


In this example..

     g1 = S(K)
     g2 = g1(K)
     result = g2('A')  --> also   S(K)(K)('A')

It depends on how it's implemented weather g1 and g2 are the same object, 
or new objects with different states.


Each ":from()" can use a full function signature, So these could be used as 
decorators.

   def continue foo:
      :from(func)
      :from(*args, **kwds)
      # do something with the function arguments
      return func(*args, **kwds)

   @foo
   def bar(...):
       ...

Or probably also be used as with statement context managers.


There is very little duplication.  The :from(...) arguments get pulled 
directly into locals, (the same as a function call updates them)..  so 
there is no assignment needed and no need to play with locals().


You could also think of this as an explicit closure turned inside out.  The 
closure become locals, and return that with each function signature.... but 
there is only one code object shared between calls, it makes writing a lot 
of things easier.

About the funny grammar...  I think it needs something different to say 
these are functions signatures.  Normally, the body is on the right of the 
colon, I used the reverse in this case because it is presenting the 
function signature from the inside, rather than the outside.  (Well, it 
makes sense to me anyway.)  I'm sure there are lots of other colours for 
this bike.

Cheers,
    Ron
















More information about the Python-ideas mailing list