Reputation: 1321
I'm studying operators in Python and came across multiple concepts that decide order of evaluation when there are multiple operators in an expression.
I understand the concept of Operator precedence, and came across the operator precedence table in Python docs. There were a few things that confused me there,
The later question stems from what the categorization of operator that I've read at various places on the internet, they categorize operators in the following categories
But when I saw keywords like lambda
, if-else
in the operator precedence table in the Python documentation, it confused me. Moreover the operator mapping table in documentation for operator module includes keywords like del
which are neither part of usual categorization on the internet and the precedence table in Python docs.
My final question is "is there any grouping that can be done about the
categories of operators and their behavior (precedence, chaining, associativity, etc) in Python? Or should I be studying every operator and its behavior independently?"
Upvotes: 0
Views: 118
Reputation: 5564
Why are assignment and augmented-assignment operators not included in the list?
Because they're not true operators. We sometimes call them operators for convenience, but they cannot form expressions and therefore do not have precedence relative to real operators.
What really counts as an operator in Python? (And is there any difference between operator and a keyword).
According to the doc on operators, it seems to be any punctuation that can form an expression. For simplicity, I prefer to define an operator as any punctuation or keyword that can form an expression.
But when I saw keywords like
lambda
,if
-else
in the operator precedence table in the Python documentation, it confused me.
Those are keywords that can form expressions, therefore they must have operator precedence. Note that if
-else
can be an expression or a block statement, depending on the syntax:
# Expression
a if condition else b
# Statement
if condition:
pass
else:
pass
Moreover the operator mapping table in documentation for operator module includes keywords like
del
which are neither part of usual categorization on the internet and the precedence table in Python docs.
del
is not an operator, because it's used only to get a side-effect. However, it can potentially modify an object in place, so it makes sense to include a function in the operator
library that does the same thing. The other use for del
is to remove a variable, which is something a function can't do.
My final question is "is there any grouping that can be done about the categories of operators and their behavior (precedence, chaining, associativity, etc) in Python? Or should I be studying every operator and its behavior independently?"
Operators can always be combined to form larger expressions, so they must have precedence and associativity to define the meaning of a non-trivial expression. Non-operator syntax usually forms either a statement or a group of statements.
Upvotes: 1