Python '+=' 和 '==+' 的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40643927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The difference between '+=' and '=+'?
提问by faceless
So I have a simple piece of code that prints out the integers 1-10:
所以我有一段简单的代码可以打印出整数 1-10:
i = 0
while i < 10:
i += 1
print(i)
Then if you just change one operator around on line 3, it prints out an infinite amount of 1 integers(which i understand why it does that). Why isn't a syntax error occurring when running this second program? Wouldn't it call a syntax error in the event of an assignment operator being followed by an addition operator??
然后,如果您只是在第 3 行更改一个运算符,它会打印出无限数量的 1 个整数(我明白为什么要这样做)。为什么在运行第二个程序时没有发生语法错误?如果赋值运算符后跟加法运算符,它不会调用语法错误吗??
i = 0
while i < 10:
i =+ 1
print(i)
回答by Kittsil
i+=1
is the same as i=i+1
, whereas
i=+1
just means i=(+1)
.
i+=1
与 相同i=i+1
,而
i=+1
只是意味着i=(+1)
。
回答by ShadowRanger
Tokenizers don't typically require spaces unless it's necessary to disambiguate (e.g. you need a space, or punctuation of some form between a variable name and a language keyword so the keyword can be recognized).
分词器通常不需要空格,除非有必要消除歧义(例如,您需要一个空格,或变量名和语言关键字之间的某种形式的标点符号,以便可以识别关键字)。
Thus, x=+y
, x =+ y
and x = +y
are all equivalent, in all cases invoking the unary +
operator on y
and assigning to x
. The unary plus operator isn't commonly used, but just because it's uncommon doesn't mean it's not recognized and accepted.
因此,x=+y
,x =+ y
和x = +y
都是等价的,在调用一元所有情况下+
运营商y
和分配到x
。一元加号运算符并不常用,但仅仅因为它不常见并不意味着它不被认可和接受。
For comparison, the -->
"operator" in C/C++ etc. is another example where humans looking for spaces and tokenizers ignoring them causes confusion.
相比之下,-->
C/C++ 等中的“运算符”是另一个示例,其中人类寻找空格而标记器忽略它们会导致混淆。
回答by Ned Batchelder
i =+ 1
is the same as i = +1
, or i = 1
.
i =+ 1
与i = +1
, 或相同i = 1
。
回答by Mercury
x=+1
is treated as: x=(+1)
while x+=1
is treated as: x=x+1
x=+1
被视为: x=(+1)
而x+=1
被视为:x=x+1
There are binary operators which operates on their left-handside operand and their right-hand side operand (e.g. * multiplication).
And there are unary operators which takes only right-hand side operand (e.g. ~/! negation).
There are operators which can be unary and binary.
有一些二元运算符对它们的左侧操作数和右侧操作数进行操作(例如 * 乘法)。
还有一元运算符只接受右侧的操作数(例如 ~/! 否定)。有可以是一元和二元的运算符。
The plus sign in python can be used also as right-hand side operator just as minus.
python 中的加号也可以用作右侧运算符,就像减号一样。
Python Docs:
Python 文档:
The unary - (minus) operator yields the negation of its numeric argument.
The unary + (plus) operator yields its numeric argument unchanged.
一元 -(减号)运算符产生其数字参数的否定。
一元 +(加号)运算符产生其数值参数不变。
回答by Fantastic Mr Fox
There is no syntax error because the expression i =+ 1
is the same as i = (+1)
and +1
is perfectly legitimate. It is a unary operator, not the addition operator.
没有语法错误,因为表达式i =+ 1
与完全相同i = (+1)
并且+1
完全合法。它是一元运算符,而不是加法运算符。