a = b = 1 just syntactic sugar?
Steven Taschuk
staschuk at telusplanet.net
Mon Jun 9 17:52:57 EDT 2003
Quoth Ed Avis:
> Steven Taschuk <staschuk at telusplanet.net> writes:
[...]
> >I actually think parentheses directly around the statement is a
> >more promising approach.
>
> I was just about to post that when I thought I should read the rest of
> your reply first ;-).
>
> Hmm, so maybe there would have to be two cases, one containing an
> expression as at present, and a second case
[...]
In full:
lambda_form ::= "lambda" [parameter_list] ":" lambda_body
lambda_body ::= "(" simple_stmt ")"
| test
(with the assumption that earlier-mentioned expansions are
preferred).
[...]
> that the two different productions for lambda overlap, after all, an
> expression can be wrapped in ( ) and so '(e)' might be the expression
> (e) or the expression_stmt e. With the idea of inserting an implicit
> 'return' before expression_stmts inside lambda functions, the
> semantics of the two would be the same so it might not matter.
Yes, I suspect you're right. It is a little ugly that you have to
special case expression_stmts, but I think this could work.
> Or maybe the bracketing could be used to avoid the 'implicit return'
> peculiarity altogether. The meaning of lambda x: expr could be
> defined to be the same as a named function containing 'return expr'.
> While the meaning of lambda x: (simple_stmt) would be defined to be
> like a named function containing simple_stmt. If we arrange that the
> first grammar rule always matches in preference to the second, then
> existing constructs like 'lambda x: (3)' would not change in meaning.
> I hope this makes some sense.
Lookahead again:
lambda x: (
might start, e.g.,
lambda x: (y) # expr
lambda x: (y = 3) # simple_stmt
But the y could be an arbitrarily complex target list.
With the proposal above, we have
lambda x: (y) # simple_stmt->expression_stmt (gets implicit rtn)
lambda x: (y = 3) # simple_stmt->assignment_stmt
No parsing problem. (Note, btw, that the grammar in the Language
Reference is not LL(1) as written, as you can see by studying
expression_stmt and assignment_stmt; contrast Grammar/Grammar.)
[...]
> Yes, life would be easier if all lambda functions currently had
> 'return expr' rather than just 'expr' as their body. [...]
*chuckle*
--
Steven Taschuk staschuk at telusplanet.net
"Telekinesis would be worth patenting." -- James Gleick
More information about the Python-list
mailing list