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

Go To StackoverFlow.com

6

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.

2012-04-04 05:10
by Words Like Jared
I'm pretty sure it's sensible to call lambda a keyword rather than an operator - A B 2012-04-04 05:13
@alberge What's your reasoning - Paragon 2012-04-04 05:14
I clearly engaged the fingers before the brain. Several python keywords are operators. They appear to be overlapping sets - A B 2012-04-04 05:17


2

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

2012-04-04 07:08
by Ben
Just because we can’t replace the 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
@poke No it's not a function, precisely because it needs an expression. You don't pass expressions to functions, you pass values. If you try to view 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
I agree: 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


4

Operators and keywords are distinct entities.

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

2012-04-04 05:16
by Ignacio Vazquez-Abrams
Why doesn't this list those as operators? It implies (informally) that it lists all tokens that are operators. Those are clearly tokens, yet aren't in the list: http://docs.python.org/py3k/reference/lexical_analysis.html#operator - Words Like Jared 2012-04-04 06:13
Simply because they are already in the list of reserved keywords - poke 2012-04-04 07:22


2

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.

2012-04-04 05:16
by Matthew Flaschen
That sentence bothers me, because of that and because it didn't give an umbrella term for INDENT, DEINDENT and NEWLINE. When explaining the notion of a Python token, I do not know if I should combine those three into a single type, or leave them separately. I guess that's irrelevant to this question, but I'm trying really hard to systematize the basics of Python - Words Like Jared 2012-04-04 05:22
Those three are all statement separators/markers. INDENT AND DEINDENT are used to mark compound statements (and function and class bodies), while NEWLINE separates simple statements - Matthew Flaschen 2012-04-04 05:25
I've been studying the grammar; Do you know the official term for it and can you cite it? The document I'm writing -- I want it to use only official jargon, if possible - Words Like Jared 2012-04-04 05:28
@WordsLikeJared, try the term "significant whitespace - Matthew Flaschen 2012-04-04 05:30
Ads