a = b = 1 just syntactic sugar?

Ed Avis ed at membled.com
Sun Jun 8 06:51:50 EDT 2003


Steven Taschuk <staschuk at telusplanet.net> writes:

>    swap = lambda L,i,j: L[i], L[j] = L[j], L[i]

As I mentioned elsewhere on this thread, lambda-functions could be
parenthesized where this is necessary.  The proposed change to have
one-line statements instead of expressions inside lambda would not
(AFAIK) make any parse that is currently unambiguous require
parentheses; only some new cases such as your example with commas
would need it.  So

    swap = lambda L,i,j: (L[i], L[j] = L[j], L[i])

>>Elsewhere you don't have to care about the difference, because any
>>expression can be used as an expression_stmt.
> 
>But not the reverse -- statements cannot be used in expressions.
>All of the following are illegal, for example:
>
>    while print x:
>        # ...
>
>    if del x[0]:
>        # ...
>
>    y = 3 + 4*(assert x > 1)

True... but somehow I recognized those as 'obviously unPythonic' and
didn't even try to write any of them.  Whereas for lambda it was a
real surprise that the rules were different from ordinary function
definitions.  I don't know, maybe some other new Python programmers
had a different experience and did have to deliberately unlearn such
constructs.

FWIW, the only one of those three that I miss is the second; it could
be useful to have del x[y] from lists and dictionaries return a value,
because it lets you easily check for unhandled keys:

    dict = {...}
    name = del dict['name']
    age = del dict['age']
    for k in dict.keys():
      logging.warning('unhandled key ' + k)

The idea is that if elsewhere in the program I add a new key but I
forget to update this code to handle it, I'll get a warning.  You can
do this at the moment, of course, but with more lines of code.  Anyway
the syntax would have to be different since del would become a member
function of the dictionary rather than a statement.

>You don't usually need to think about the statement/expression
>distinction, I agree, but *not* because they're interchangeable in
>most contexts -- that's simply false.  It's that the division between
>them is almost perfectly done,

I don't know about perfect - 'print' seems rather an anomaly, and
assignment to list and dictionary elements is beginning to look like a
strange special case - but I agree that most of the time it works
fairly well.  Lambda is the only real area where it starts to produce
'surprising' results, and that only because lambda is synactically
restricted to contain only expressions.

>If there is a problem here, imho it is that lambda makes people want
>to put statements in expressions, thereby bringing to centre stage a
>distinction which is usually unobtrusive.  Anonymity is a red
>herring; so are closures; the important point is that lambdas are
>expressions and defs are statements.

Well a named function has two halves: the 'def f: ...' which is a
statement, and the use of the name 'f' which is an expression.  Lambda
combines both in one place.  Letting it contain one-line statements
seems like the best compromise between the two worlds.  Expressions
only is too counterintuitive IMHO.

-- 
Ed Avis <ed at membled.com>




More information about the Python-list mailing list