考虑这些表达式…请耐心等待…这是一个很长的清单…
(注意:有些表达式是重复的-只是为了表示一个“上下文”)
a, b = 1, 2 # simple sequence assignment a, b = ['green', 'blue'] # list asqignment a, b = 'XY' # string assignment a, b = range(1,5,2) # any iterable will do # nested sequence assignment (a,b), c = "XY", "Z" # a = 'X', b = 'Y', c = 'Z' (a,b), c = "XYZ" # ERROR -- too many values to unpack (a,b), c = "XY" # ERROR -- need more than 1 value to unpack (a,b), c, = [1,2],'this' # a = '1', b = '2', c = 'this' (a,b), (c,) = [1,2],'this' # ERROR -- too many values to unpack # extended sequence unpacking a, *b = 1,2,3,4,5 # a = 1, b = [2,3,4,5] *a, b = 1,2,3,4,5 # a = [1,2,3,4], b = 5 a, *b, c = 1,2,3,4,5 # a = 1, b = [2,3,4], c = 5 a, *b = 'X' # a = 'X', b = [] *a, b = 'X' # a = [], b = 'X' a, *b, c = "XY" # a = 'X', b = [], c = 'Y' a, *b, c = "X...Y" # a = 'X', b = ['.','.','.'], c = 'Y' a, b, *c = 1,2,3 # a = 1, b = 2, c = [3] a, b, c, *d = 1,2,3 # a = 1, b = 2, c = 3, d = [] a, *b, c, *d = 1,2,3,4,5 # ERROR -- two starred expressions in assignment (a,b), c = [1,2],'this' # a = '1', b = '2', c = 'this' (a,b), *c = [1,2],'this' # a = '1', b = '2', c = ['this'] (a,b), c, *d = [1,2],'this' # a = '1', b = '2', c = 'this', d = [] (a,b), *c, d = [1,2],'this' # a = '1', b = '2', c = [], d = 'this' (a,b), (c, *d) = [1,2],'this' # a = '1', b = '2', c = 't', d = ['h', 'i', 's'] *a = 1 # ERROR -- target must be in a list or tuple *a = (1,2) # ERROR -- target must be in a list or tuple *a, = (1,2) # a = [1,2] *a, = 1 # ERROR -- 'int' object is not iterable *a, = [1] # a = [1] *a = [1] # ERROR -- target must be in a list or tuple *a, = (1,) # a = [1] *a, = (1) # ERROR -- 'int' object is not iterable *a, b = [1] # a = [], b = 1 *a, b = (1,) # a = [], b = 1 (a,b),c = 1,2,3 # ERROR -- too many values to unpack (a,b), *c = 1,2,3 # ERROR - 'int' object is not iterable (a,b), *c = 'XY', 2, 3 # a = 'X', b = 'Y', c = [2,3] # extended sequence unpacking -- NESTED (a,b),c = 1,2,3 # ERROR -- too many values to unpack *(a,b), c = 1,2,3 # a = 1, b = 2, c = 3 *(a,b) = 1,2 # ERROR -- target must be in a list or tuple *(a,b), = 1,2 # a = 1, b = 2 *(a,b) = 'XY' # ERROR -- target must be in a list or tuple *(a,b), = 'XY' # a = 'X', b = 'Y' *(a, b) = 'this' # ERROR -- target must be in a list or tuple *(a, b), = 'this' # ERROR -- too many values to unpack *(a, *b), = 'this' # a = 't', b = ['h', 'i', 's'] *(a, *b), c = 'this' # a = 't', b = ['h', 'i'], c = 's' *(a,*b), = 1,2,3,3,4,5,6,7 # a = 1, b = [2, 3, 3, 4, 5, 6, 7] *(a,*b), *c = 1,2,3,3,4,5,6,7 # ERROR -- two starred expressions in assignment *(a,*b), (*c,) = 1,2,3,3,4,5,6,7 # ERROR -- 'int' object is not iterable *(a,*b), c = 1,2,3,3,4,5,6,7 # a = 1, b = [2, 3, 3, 4, 5, 6], c = 7 *(a,*b), (*c,) = 1,2,3,4,5,'XY' # a = 1, b = [2, 3, 4, 5], c = ['X', 'Y'] *(a,*b), c, d = 1,2,3,3,4,5,6,7 # a = 1, b = [2, 3, 3, 4, 5], c = 6, d = 7 *(a,*b), (c, d) = 1,2,3,3,4,5,6,7 # ERROR -- 'int' object is not iterable *(a,*b), (*c, d) = 1,2,3,3,4,5,6,7 # ERROR -- 'int' object is not iterable *(a,*b), *(c, d) = 1,2,3,3,4,5,6,7 # ERROR -- two starred expressions in assignment *(a,b), c = 'XY', 3 # ERROR -- need more than 1 value to unpack *(*a,b), c = 'XY', 3 # a = [], b = 'XY', c = 3 (a,b), c = 'XY', 3 # a = 'X', b = 'Y', c = 3 *(a,b), c = 'XY', 3, 4 # a = 'XY', b = 3, c = 4 *(*a,b), c = 'XY', 3, 4 # a = ['XY'], b = 3, c = 4 (a,b), c = 'XY', 3, 4 # ERROR -- too many values to unpack
你如何理解这种复杂性和混乱性。手工计算表达式的结果时,如何总是对的。或者,当阅读其他人的代码时,我应该只是忽略它们,而从不试图理解表达式的实际作用吗?
对于这篇文章的篇幅,我深表歉意,但我决定选择完整性。
一旦你了解了一些基本规则,就不难概括它们。我将尽力举例说明。由于你是在谈论“手工”评估,因此,我将建议一些简单的替换规则。基本上,如果所有可迭代对象的格式都相同,则可能会更容易理解表达式。
仅出于解压缩的目的,以下替换在()的右侧有效=(即,对于rvalues):
'XY' -> ('X', 'Y') ['X', 'Y'] -> ('X', 'Y')
如果发现值没有解包,则将撤消替换。(有关更多说明,请参见下文。)
另外,当你看到“裸”逗号时,请假装有一个顶级元组。在左侧和右侧都执行此操作(即,对于lvalues和rvalues):
'X', 'Y' -> ('X', 'Y') a, b -> (a, b)
考虑到这些简单的规则,下面是一些示例:
(a,b), c = "XY", "Z" # a = 'X', b = 'Y', c = 'Z'
应用上述规则,我们将转换"XY"为('X', 'Y'),并用括号覆盖裸逗号:
"XY"
('X', 'Y')
((a, b), c) = (('X', 'Y'), 'Z')
这里的视觉对应关系使分配工作原理非常明显。
这是一个错误的示例:
(a,b), c = "XYZ"
按照上述替换规则,我们得到以下内容:
((a, b), c) = ('X', 'Y', 'Z')
这显然是错误的;嵌套结构不匹配。现在,让我们来看一个稍微复杂的示例的工作方式:
(a,b), c, = [1,2],'this' # a = '1', b = '2', c = 'this'
应用上述规则,我们得到
((a, b), c) = ((1, 2), ('t', 'h', 'i', 's'))
但是现在从结构上很明显,它’this’不会被解压缩,而是直接分配给c。因此,我们撤消替换。
((a, b), c) = ((1, 2), 'this')
现在,让我们看一下在包装c元组时会发生什么:
(a,b), (c,) = [1,2],'this' # ERROR -- too many values to unpack
成为
((a, b), (c,)) = ((1, 2), ('t', 'h', 'i', 's'))
同样,该错误是显而易见的。c不再是裸变量,而是序列中的变量,因此右侧的相应序列被解包为(c,)。但是序列的长度不同,因此会出现错误。
现在使用*操作员扩展拆箱。这有点复杂,但仍然相当简单。*开头的变量将成为一个列表,其中包含相应序列中未分配给变量名称的所有项目。从一个非常简单的示例开始:
*
a, *b, c = "X...Y" # a = 'X', b = ['.','.','.'], c = 'Y'
这变成
(a, *b, c) = ('X', '.', '.', '.', 'Y')
分析此问题的最简单方法是从头开始工作。’X’被分配给a并’Y’分配给c。序列中的其余值将放入列表中并分配给b。
Lvalue像(a, b)和(a, b)只是上述情况的特例。一个左值序列内不能有两个运算符,因为这会造成歧义。该值会去哪里在这样的事情(a, b, *c, d)-在b或c?一会儿我将考虑嵌套案例。
*a = 1 # ERROR -- target must be in a list or tuple
这里的错误是不言自明的。目标(*a)必须位于元组中。
*a, = (1,2) # a = [1,2]
这是有效的,因为有一个赤裸的逗号。正在应用规则…
(*a,) = (1, 2)
由于除之外没有其他变量a,a所以将rvalue序列中的所有值都吸收掉。如果(1, 2)用单个值替换,该怎么办?
*a, = 1 # ERROR -- 'int' object is not iterable
变成
(*a,) = 1
同样,这里的错误是不言自明的。你不能解压缩不是序列的*a东西,而需要解压缩东西。所以我们把它放在一个序列中
*a, = [1] # a = [1]
相当于
(*a,) = (1,)
最后,这是一个常见的混淆点:(1)与1- 一样,你需要使用逗号将元组与算术语句区分开。
*a, = (1) # ERROR -- 'int' object is not
现在进行嵌套。实际上,此示例不在你的“嵌套”部分中;也许你没有意识到它是嵌套的?
(a,b), *c = 'XY', 2, 3 # a = 'X', b = 'Y', c = [2,3] 成为 ((a, b), *c) = (('X', 'Y'), 2, 3)
就像我们所期望的那样,顶级元组中的第一个值被分配,而顶级元组(2和3)中的其余值被分配给c。
(a,b),c = 1,2,3 # ERROR -- too many values to unpack *(a,b), c = 1,2,3 # a = 1, b = 2, c = 3
我已经在上面解释了为什么第一行引发错误。第二行很愚蠢,但这是它起作用的原因:
(*(a, b), c) = (1, 2, 3)
如前所述,我们从头开始。3被分配给c,然后将剩余的值被分配给具有可变*它前面,在这种情况下,(a, b)。因此,这等效于(a, b) = (1, 2),由于元素数量正确,因此之所以起作用。我不认为这会在工作代码中出现任何原因。同样,
*(a, *b), c = 'this' # a = 't', b = ['h', 'i'], c = 's' 变成 (*(a, *b), c) = ('t', 'h', 'i', 's')
从头开始工作,’s’分配给c,并(‘t’, ‘h’, ‘i’)分配给(a, *b)。从头开始再次工作,’t’被分配给a,并被(‘h’, ‘i’)分配给b作为列表。这是另一个愚蠢的示例,永远不要出现在工作代码中。