python 中 += 语句的句法结构是什么?

What is the syntactical structure of a += statement in python?

我今天在 python 中发现了一些东西。但是还没有找到明确的解释。

在 python 中似乎可行:

variable += a_single_statement

所以,下列说法是正确的:

variable += another_variable
variable += (another_variable - something_else)

但是下面的做法是不正确的:

variable += a_variable - b_variable

有人可以解释为什么会这样吗,最好是 link 到语法结构的文档,解释 += 运算符的操作数是什么,预期的表达式是什么以及什么他们的结构是?另外,我上面概述的想法是否正确?

行为似乎与我使用的其他编程语言不同,最后 'statement' 导致语法错误。

编辑:它不起作用的代码。它可能是一个空白错误:/

T = input()
counter = 0

# For each word, figure out edit length to palindrome
for _ in range(T):
    counter += 1
    word = raw_input()
    word_len = len(word) #stored for efficiency
    index = 0
    sum_edits = 0

    # Iterate half the word and always compare characters 
    # at equal distance d from the beginning and from 
    # the ending of the word
    while index < word_len/2.0:
        sum_edits += max(ord(word[index]), ord(word[word_len-index-1])) -
                min(ord(word[index]), ord(word[word_len - index - 1]))
        index += 1

    print sum_edits

如果您只能将字母 'downwards' 更改为 'a'.

,则该代码可以检测将单词变成回文需要编辑多少次

这是否意味着您不能在 python 代码中任意分行,如果很明显 'expression' 无论如何都必须继续?或者,如果代码行被括号括起来,您是否可以只将它们分开? 抱歉,我是 python.

的新手

+=无关。只是 Python 不允许您将语句拆分为多行,除非有一个开放的 ({[,或者除非您使用\。这两行应该是一个语句可能看起来很明显,但是当你有像

这样的语句时
a = loooooooooooooooooooooooooooooong_thiiiiiiiiiiiiiiiiiiiiiiiing
  + ooooooooooooootheeeeeeeeeeeeer_thiiiiiiiiiiiiiiiiiiiing

这是一种说法还是两种说法?如果你允许

a = loooooooooooooooooooooooooooooong_thiiiiiiiiiiiiiiiiiiiiiiiing +
    ooooooooooooootheeeeeeeeeeeeer_thiiiiiiiiiiiiiiiiiiiing

是一个语句,那么在第二行使用 + 运算符的任何一种解释都是令人困惑且容易出错的。 Javascript 试图允许这种事情,它的分号插入会导致各种问题。

如果您还没有在方括号或大括号内,通常建议使用圆括号。