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 lowest-priority operator in Python.

And according to this: http://docs.python.org/py3k/reference/lexical_analysis.html#keywords

lambda is a Python keyword.

HOWEVER, according to this: http://docs.python.org/py3k/reference/lexical_analysis.html#other-tokens

Operators and keywords are different objects.

I am trying to systematically explain Python 3.2 to someone, and I do not want to confuse them. I myself am confused, however, about 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 and the Python vocabulary.

+6
source share
3 answers

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

+2
source

Operators and keywords are different objects.

No, it is not. is , in , and , or , not , and I'm sure a little more.

+4
source

This does not say that they are mutually exclusive, simply:

"The following categories of tokens exist: identifiers, keywords, literals, operators, and delimiters"

I am in the categories of man and american. Of course, this may mean that they are mutually exclusive, in which case the documents are incompatible.

+2
source

Source: https://habr.com/ru/post/912356/


All Articles