问题
Most sources online call = (and +=, -=, etc...) an assignment operator (for python). This makes sense in most languages, however, not in python. An operator takes one or more operands, returns a value, and forms an expression. However, in python, assignment is not an expression, and assignment does not yield a value. Therefore, = cannot be an operator.
So what exactly is it? In a statement like x = 0, x is an identifier, 0 is a numeric literal, but I don't know what to call "=".
回答1:
I was able to find the correct answer in the official python documentation. = and friends are considered delimiters. source: https://docs.python.org/3/reference/lexical_analysis.html#delimiters
python docs reference for expressions does not define = as an operator nor as forming an expression. source: https://docs.python.org/3/reference/expressions.html
It does, however, define assignment statements with their own production rule with = explicitly included in the rule. source: https://docs.python.org/3/reference/simple_stmts.html#assignment-statements
So the final answer is that it is "delimiter" according to official sources.
回答2:
The assignment symbol =
behaves like a statement, not as an operator. It supports chaining as part of the syntax but cannot be used as an operation (e.g. a = b = 0
but not if a = b:
).
It is similar to the in
part of a for ... in ...:
statement. That in
is part of the statement syntax, it is not the actual in
operator.
来源:https://stackoverflow.com/questions/56755308/what-actually-is-the-assignment-symbol-in-python