多重賦值語句箕速,不是說x=y, y=x+y=y+y,而是把右邊的變量賦給臨時(shí)變量后叼风,再做賦值(In an assignment statement, the right-hand side is always evaluated fully before doing the actual setting of variables):
x, y = y, x + y
等價(jià)于:
ham = y
spam = x + y
x = ham
y = spam
比如反轉(zhuǎn)列表, 都不用考慮臨時(shí)變量的存儲,因?yàn)槎嘀刭x值語句自動幫我們存了:
def reverseList(self, head):
cur, pre = head, None
while cur:
cur.next, prev, cur = pre, cur, cur.next
return prev
此外以下賦值語句也關(guān)注一下:
a, a[0] = [1], 2
# a == [2]