Let's Talk About Lambda Functions!
bokr at oz.net
Sat Aug 3 05:39:35 CEST 2002
On Sat, 3 Aug 2002 01:59:40 +0000 (UTC), huaiyu at gauss.almadan.ibm.com (Huaiyu Zhu) wrote:
>Bengt Richter <bokr at oz.net> wrote:
>>Just leave out the name after the def to make it anonymous,
>>and use parens to make the scope of the def expression obvious, e.g.,
>> x = y + (
>> def (z):
>> if z: return 'True z'
>> else: return 'False z'
>> )('arg for anonymous function') + ' & additional expression terms.'
>One problem of allowing intervening parens to adjust indentation is that the
>following is also a possibility
No, the parens don't adjust the indentation, they allow the *def* to determine it freely.
After the paren preceding the def, the def defines the reference for indentation of its suite
> x = y + (
> if z: return 'True z'
> else: return 'False z'
> )('arg for anonymous function') + ' & additional expression terms.'
^--this would be a syntax error, since it is indented to the right of the def
and would be parsed as if it were another line of the code of the function,
(and for that it is both the wrong indentation and a bad expression).
The closing ')' is only legal if under, or to the left of, the 'd' in def, so it
signals the final dedent. I.e.,
x = y + (
if z: return 'True z'
else: return 'False z'
)('arg for anonymous function') + ' & additional expression terms.'
would be legal, though not pretty.
>I think it's better to ban this by completely ignore intervening parens as
>far as indentation is concerned.
They effectively _are_ ignored until the closing one is at <= def indentation.
That's the point of having the parens -- so the def and suite can be parsed
exactly as if in a normal block.
I hope that makes it clearer. I chose the indentation of the def for appearance,
with no required relation to the position of the preceding '(' or anything on that line.
Only the final ')' has the position requirement that it be dedented to or to the left of the def.
More information about the Python-list