In Python 3.2, according to this: http://docs.python.org/py3k/reference/expressions.html#summary
lambda
is the operator with the lowest precedence in Python.
And according to this: http://docs.python.org/py3k/reference/lexical_analysis.html#keywords
lambda
is a Python language keyword.
HOWEVER, according to this: http://docs.python.org/py3k/reference/lexical_analysis.html#other-tokens
Operators and keywords are distinct entities.
I'm trying to systematically explain Python 3.2 to someone and I don't want to confuse them. I, myself, am confused, though, on the exact definitions of operators and keywords.
My best guess is that the term "operator" means something slightly different when used in the context of the Python parser versus the Python lexer.
lambda
clearly is a keyword; it's a special word recognised by the parser, which would otherwise fall within the definition of an identifier
.
lambda
is not semantically an operator. An operator is just a function, but invoked with a different syntax. We can imagine replacing the +
operator with an add
function; all our programs with addition would get more verbose and harder to read, but we could still write them. The lambda
language construct on the other hand could not be replaced with a function; lambda x: x+1
is not just calculating a 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 to the function being defined, and x+1
is the code of the lambda body).
In the same page you linked to we have: http://docs.python.org/py3k/reference/lexical_analysis.html#operators
The following tokens are operators:
+ - * ** / // % << >> & | ^ ~ < > <= >= == !=
That's the entire contents of the subsection on operators. From this I take it to mean that in the context of defining the tokens of the language "operators" are symbolic operators, whereas the section on keywords is explicitly spelling out that "these things which would otherwise be identifiers are keywords". That's why I think the keyword operators like not
, is
, in
, etc are not listed. But there certainly are things that are semantically operators that are keywords, whether or not the parser considers them separate classes.
I'm not sure why http://docs.python.org/py3k/reference/expressions.html#summary describes lambda
as an operator; I certainly wouldn't. Strictly speaking it doesn't explicitly say "lambda
is the operator with the lowest precedence", it just lists lambda
in a table whose column heading is "Operator". Perhaps it was just a convenience; describing lambda
as a thing with low precedence is a good way of clarifying how Python will parse lambda x: x + 1
(it could theoretically be either (lambda x: x) + 1
or lambda x: (x + 1)
).
lambda
construct with a different function, it does not make lambda
not a function. It is a somewhat unary function (if you take the parameter matcher out) that takes an expression and returns a function. It is clearly an operator as it takes an expression and returns an expression (of type function) - poke 2012-04-04 07:19
lambda
you don't "pass" it a value, you pass it an expression (which cannot be "passed" to any other kind of function). Otherwise it would be valid to bind the expression to a name and pass the name instead: x = fire_ze_missiles(); f = lambda: x
is very different from f = lambda: fire_ze_missiles()
, while x = fire_ze_missiles(); y = x + 1
is equivalent to y = fire_ze_missiles() + 1
- Ben 2012-04-04 08:02
lambda
does not "smell like" an operator. And as you note precedence tables are not just for operators: they're really a way for humans to deal with binding-strengths when looking at what would otherwise be an ambiguous parse. We're merely used to seeing such ambiguity when dealing with expressions-with-operators - torek 2012-04-04 08:48
Operators and keywords are distinct entities.
No they're not. is
, in
, and
, or
, not
, and I'm sure a few more are both.
That doesn't say they're mutually exclusive, just that:
"the following categories of tokens exist: identifiers, keywords, literals, operators, and delimiters"
I'm in the categories person and American. Granted, it may mean to imply that they're mutually exclusive, in which case the docs are inconsistent.
lambda
a keyword rather than an operator - A B 2012-04-04 05:13