[Python-ideas] A "local" pseudo-function
Tim Peters
tim.peters at gmail.com
Sun Apr 29 15:55:33 EDT 2018
[Tim]
>> Then `c` is 12, but `a` is still 1 and `b` is still 2. Same thing in the end:
>>
>> c = local(a=3, b=4, a*b)
[Nikolaus Rath <Nikolaus at rath.org>]
> I think this can be done already with slighly different syntax:
>
> c = (lambda a=3, b=4: a*b)()
>
> The trailing () is a little ugly, but the semantics are much more
> obvious.
But also broken, in a way that can't be sanely fixed. Covered before
in other messages. Short course:
>>> a = 10
>>> b = 20
>>> (lambda a=3, b=a+1: (a, b))()
(3, 11)
This context really demands (3, 4) instead. In Scheme terms, Python's
lambda default arguments do "let" binding ("all at once"), but "let*"
binding is what's needed ("one at a time, left to right, with bindings
already done visible to later bindings").
Of course in Scheme you explicitly type either "let" or "let*" (or
"letrec", or ...) depending on what you want at the time, but "let*"
is overwhelmingly what's wanted when it makes a difference (in the
example at the top, it makes no difference at all). Otherwise you
can't build up a complex result from little named pieces that may
depend on pieces already defined. See,.e.g, the quadratic equation
example in the original post, where this was implicit.
> ...
More information about the Python-ideas
mailing list