[Python-ideas] One more time... lambda function <--- from *** signature def.
Steven D'Aprano
steve at pearwood.info
Tue Mar 4 12:23:20 CET 2014
On Tue, Mar 04, 2014 at 11:03:35AM +0200, David Townshend wrote:
> My suggestion was to use c'x + 3' as an effective replacement for
> compile('x + 3', '', 'eval'). The use of a literal rather than compile
> would solve a few of the issues raised in Steven's original post. I don't
> see why it would be any more misleading to eval a code literal than a
> compiled string.
Agreed.
> I did suggest making it callable (i.e. thunk(x=3), but on
> second thoughts I'm not sure that's a good idea. It adds complexity
> without and real benefit over eval.
I don't think calling the thunk is the right API. If we have this sort
of functionality, I want calling a thunk to be treated like any other
expression, thunk[0] or thunk+1. I don't want it to mean "evaluate this
thunk". An example:
thunk = `lambda x: x + y`
def func(y, obj):
return obj + 1
result = func(50, thunk(100))
(This is not a use-case, just an illustration.)
In this example, the expression "thunk(1000)" is delayed until inside
the func scope. Inside that scope, we evaluate this expression:
(lambda x: x + y)(100)
where y has the value 50, and bind the result of this to the name obj.
The lambda part returns a closure, and calling that closure with
argument 100 returns 150, so this is equivalent to binding obj=150. Then
func continues, evaluates "obj + 1", and returns 151.
--
Steven
More information about the Python-ideas
mailing list