Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Python 3.2, is "lambda" considered a "keyword," an "operator" or both?

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.

like image 354
Jesus is Lord Avatar asked Apr 04 '12 05:04

Jesus is Lord


People also ask

What is lambda in Python 3?

A lambda function is a small anonymous function. A lambda function can take any number of arguments, but can only have one expression.

Can we assign value in lambda function Python?

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).

What is the is in Python?

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.


2 Answers

Operators and keywords are distinct entities.

No they're not. is, in, and, or, not, and I'm sure a few more are both.

like image 119
Ignacio Vazquez-Abrams Avatar answered Sep 18 '22 14:09

Ignacio Vazquez-Abrams


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)).

like image 37
Ben Avatar answered Sep 21 '22 14:09

Ben