[Python-ideas] A "local" pseudo-function
Greg Ewing
greg.ewing at canterbury.ac.nz
Tue May 1 01:49:38 EDT 2018
Tim Peters wrote:
> I expected the following would work, but it doesn't :-)
>
> iseven = lambda n: (
> lambda n=n, \
> even = (lambda n: n == 0 or odd(n-1)), \
> odd = (lambda n: False if n == 0 else even(n-1)):
> even(n))()
>
> Ugly and obscure, but why not? In the inner lambda, `n`, `even`, and
> `odd` are all defined in its namespace,
But 'even' and 'odd' not defined in the environment of the lambdas
assigned to them, because default values of a function's arguments
are evaluated outside of that function.
> For `even` to know at compile-time that `odd` will show up later in
> its enclosing lambda's arglist requires that Python do `letrec`-style
> binding instead. For a start ;-)
One could envisage adding a letrec-like construct, but making the
argument list of an ordinary lambda behave like a letrec would be
warping things rather too much, IMO.
Personally I'd rather just add a "where" clause, and backwards
compatibility be damned. :-)
--
Greg
More information about the Python-ideas
mailing list