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.
A lambda function is a small anonymous function. A lambda function can take any number of arguments, but can only have one expression.
Syntax. Simply put, a lambda function is just like any normal python function, except that it has no name when defining it, and it is contained in one line of code. A lambda function evaluates an expression for a given argument. You give the function a value (argument) and then provide the operation (expression).
The is keyword is used to test if two variables refer to the same object. The test returns True if the two objects are the same object. The test returns False if they are not the same object, even if the two objects are 100% equal. Use the == operator to test if two variables are equal.
Operators and keywords are distinct entities.
No they're not. is
, in
, and
, or
, not
, and I'm sure a few more are both.
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)
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With