lambda
is a keyword; this is a special word recognized by the parser, which otherwise would fall into the definition of identifier
.
lambda
not a semantic statement. An operator is just a function, but called with a different syntax. We can imagine replacing the +
operator with the add
function; all of our programs with the addition will be more detailed and difficult to read, but we could still write them. On the other hand, the lambda
language construct cannot be replaced by a function; lambda x: x+1
is not just calculating the result from the values x
and x+1
, because in this context they are not values at all ( x
is the name of the parameter for the function being defined and x+1
is the lambda body code).
On the same page that you contacted: http://docs.python.org/py3k/reference/lexical_analysis.html#operators
The following tokens are operators:
+ - * ** / // % << >> & | ^ ~ < > <= >= == !=
That the entire contents of the subsection are operators. From this, I mean that in the context of defining language tokens, “operators” are symbolic operators, while the keyword section explicitly states that “these things, which would otherwise be identifiers, would be keywords”. Therefore, I think that keyword operators are like not
, is
, in
, etc. Not specified. But, of course, there are things that are semantic operators that are keywords, regardless of whether the parser considers them to be separate classes.
I'm not sure why http://docs.python.org/py3k/reference/expressions.html#summary describes lambda
as an operator; Of course, I would not. Strictly speaking, he does not explicitly say: " lambda
is the operator with the lowest priority", he simply lists lambda
in the table whose column heading is "Operator". Perhaps it was just convenience; describing lambda
as a low priority thing is a good way to clarify how Python will parse lambda x: x + 1
(theoretically it could be either (lambda x: x) + 1
or lambda x: (x + 1)
).
source share