解包、扩展解包和嵌套扩展解包

发布于 2025-01-20 18:10:04 字数 4654 浏览 3 评论 0原文

考虑以下表达式。请注意,重复一些表达式以呈现“上下文”。

(这是一个长列表)

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

如何手工正确地推断出这种表达式的结果?

Consider the following expressions. Note that some expressions are repeated to present the "context".

(this is a long list)

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

How to correctly deduce the result of such expressions by hand?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

坏尐絯℡ 2025-01-27 18:10:04

我对这篇文章的长度表示歉意,但我决定选择完整性。

一旦知道了一些基本规则,就不难概括它们。我会尽力解释一些例子。由于您谈论的是“手工评估这些”,因此我建议一些简单的替代规则。基本上,您可能会发现,如果以相同的方式对所有迭代进行格式化,则更容易理解表达式。

的右侧是有效的

'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'),并覆盖Parens中的裸逗号:

((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

( *a,b)之类的lvalues(a, *b)只是上述特殊情况。您不能在一个lvalue序列中有两个*运算符,因为这是模棱两可的。这些值在(a, *b, *c,d)之类的东西中会在哪里进行, - 在bc中?我将在稍后考虑嵌套的情况。

*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)

顶级元组中的第一个值,并将其剩余值分配(23)被分配给c /code> - 正如我们的期望一样。

(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列表。这是另一个愚蠢的示例,不应在工作代码中出现。

My apologies for the length of this post, but I decided to opt for completeness.

Once you know a few basic rules, it's not hard to generalize them. I'll do my best to explain with a few examples. Since you're talking about evaluating these "by hand," I'll suggest some simple substitution rules. Basically, you might find it easier to understand an expression if all the iterables are formatted in the same way.

For the purposes of unpacking only, the following substitutions are valid on the right side of the = (i.e. for rvalues):

'XY' -> ('X', 'Y')
['X', 'Y'] -> ('X', 'Y')

If you find that a value doesn't get unpacked, then you'll undo the substitution. (See below for further explanation.)

Also, when you see "naked" commas, pretend there's a top-level tuple. Do this on both the left and the right side (i.e. for lvalues and rvalues):

'X', 'Y' -> ('X', 'Y')
a, b -> (a, b)

With those simple rules in mind, here are some examples:

(a,b), c = "XY", "Z"                 # a = 'X', b = 'Y', c = 'Z'

Applying the above rules, we convert "XY" to ('X', 'Y'), and cover the naked commas in parens:

((a, b), c) = (('X', 'Y'), 'Z')

The visual correspondence here makes it fairly obvious how the assignment works.

Here's an erroneous example:

(a,b), c = "XYZ"

Following the above substitution rules, we get the below:

((a, b), c) = ('X', 'Y', 'Z')

This is clearly erroneous; the nested structures don't match up. Now let's see how it works for a slightly more complex example:

(a,b), c, = [1,2],'this'             # a = '1', b = '2', c = 'this'

Applying the above rules, we get

((a, b), c) = ((1, 2), ('t', 'h', 'i', 's'))

But now it's clear from the structure that 'this' won't be unpacked, but assigned directly to c. So we undo the substitution.

((a, b), c) = ((1, 2), 'this')

Now let's see what happens when we wrap c in a tuple:

(a,b), (c,) = [1,2],'this'           # ERROR -- too many values to unpack

Becomes

((a, b), (c,)) = ((1, 2), ('t', 'h', 'i', 's'))

Again, the error is obvious. c is no longer a naked variable, but a variable inside a sequence, and so the corresponding sequence on the right is unpacked into (c,). But the sequences have a different length, so there's an error.

Now for extended unpacking using the * operator. This is a bit more complex, but it's still fairly straightforward. A variable preceded by * becomes a list, which contains any items from the corresponding sequence that aren't assigned to variable names. Starting with a fairly simple example:

a, *b, c = "X...Y"                   # a = 'X', b = ['.','.','.'], c = 'Y'

This becomes

(a, *b, c) = ('X', '.', '.', '.', 'Y')

The simplest way to analyze this is to work from the ends. 'X' is assigned to a and 'Y' is assigned to c. The remaining values in the sequence are put in a list and assigned to b.

Lvalues like (*a, b) and (a, *b) are just special cases of the above. You can't have two * operators inside one lvalue sequence because it would be ambiguous. Where would the values go in something like this (a, *b, *c, d) -- in b or c? I'll consider the nested case in a moment.

*a = 1                               # ERROR -- target must be in a list or tuple

Here the error is fairly self-explanatory. The target (*a) must be in a tuple.

*a, = (1,2)                          # a = [1,2]

This works because there's a naked comma. Applying the rules...

(*a,) = (1, 2)

Since there are no variables other than *a, *a slurps up all the values in the rvalue sequence. What if you replace the (1, 2) with a single value?

*a, = 1                              # ERROR -- 'int' object is not iterable

becomes

(*a,) = 1

Again, the error here is self-explanatory. You can't unpack something that isn't a sequence, and *a needs something to unpack. So we put it in a sequence

*a, = [1]                            # a = [1]

Which is eqivalent to

(*a,) = (1,)

Finally, this is a common point of confusion: (1) is the same as 1 -- you need a comma to distinguish a tuple from an arithmetic statement.

*a, = (1)                            # ERROR -- 'int' object is not 

Now for nesting. Actually this example wasn't in your "NESTED" section; perhaps you didn't realize it was nested?

(a,b), *c = 'XY', 2, 3               # a = 'X', b = 'Y', c = [2,3]

Becomes

((a, b), *c) = (('X', 'Y'), 2, 3)

The first value in the top-level tuple gets assigned, and the remaining values in the top-level tuple (2 and 3) are assigned to c -- just as we should expect.

(a,b),c = 1,2,3                      # ERROR -- too many values to unpack
*(a,b), c = 1,2,3                    # a = 1, b = 2, c = 3

I've already explained above why the first line throws an error. The second line is silly but here's why it works:

(*(a, b), c) = (1, 2, 3)

As previously explained, we work from the ends. 3 is assigned to c, and then the remaining values are assigned to the variable with the * preceding it, in this case, (a, b). So that's equivalent to (a, b) = (1, 2), which happens to work because there are the right number of elements. I can't think of any reason this would ever appear in working code. Similarly,

*(a, *b), c = 'this'                 # a = 't', b = ['h', 'i'], c = 's'

becomes

(*(a, *b), c) = ('t', 'h', 'i', 's')

Working from the ends, 's' is assigned to c, and ('t', 'h', 'i') is assigned to (a, *b). Working again from the ends, 't' is assigned to a, and ('h', 'i') is assigned to b as a list. This is another silly example that should never appear in working code.

陌伤ぢ 2025-01-27 18:10:04

我发现Python 2元组开箱非常简单。左侧的每个名称都与右侧的序列中的整个序列或单个项目相对应。如果名称对应于任何序列的单个项目,则必须有足够的名称来覆盖所有项目。

但是,扩展的解开包装肯定会令人困惑,因为它是如此强大。现实是,您绝对不应该执行最后10个或更多的有效示例 - 如果数据是结构化的,则应该在dict或类实例中,而不是诸如列表之类的非结构化表单。

显然,可以滥用新的语法。您问题的答案是,您不应该阅读这样的表达方式 - 它们是不好的练习,我怀疑它们会被使用。

仅仅因为您可以编写任意复杂的表达方式并不意味着您应该。您可以编写MAP(MAP,ITOBLOS_OF_TRANSFORMATIONS,MAP(MAP,ITOBLOCT_OF_TRANSFORMATIONS,itoble_of_iterables_of_iterables))的代码),但您 not

I find the Python 2 tuple unpacking pretty straightforward. Each name on the left corresponds with either an entire sequence or a single item in a sequence on the right. If names correspond to single items of any sequence, then there must be enough names to cover all of the items.

Extended unpacking, however, can certainly be confusing, because it is so powerful. The reality is you should never be doing the last 10 or more valid examples you gave -- if the data is that structured, it should be in a dict or a class instance, not unstructured forms like lists.

Clearly, the new syntax can be abused. The answer to your question is that you shouldn't have to read expressions like that -- they're bad practice and I doubt they'll be used.

Just because you can write arbitrarily complex expressions doesn't mean you should. You could write code like map(map, iterable_of_transformations, map(map, iterable_of_transformations, iterable_of_iterables_of_iterables)) but you don't.

行雁书 2025-01-27 18:10:04

我认为您的代码可能会误导其他形式来表达它。

这就像在表达式中使用额外的括号来避免有关操作员优先级的问题。
我总是一项不错的投资,以使您的代码可读。

我更喜欢仅用于诸如交换之类的简单任务。

I you think your code may be misleading use other form to express it.

It's like using extra brackets in expressions to avoid questions about operators precedence.
I'ts always a good investment to make your code readable.

I prefer to use unpacking only for simple tasks like swap.

巷雨优美回忆 2025-01-27 18:10:04

lhs 处加星号表达式的最初想法是为了提高可迭代解包的可读性,如下所示:

first_param, rest_param, third_param = param[0], param[1:-1], param[-1]

该语句相当于

first_param, *rest_param, third_param = param

在上面的语句中,加星号表达式用于“捕获”所有未分配给“强制目标”的元素(本例中的 first_paramthird_param)

在 lhs 处使用加星号表达式有以下规则:

  1. 最多加一个星号lhs 处的表达式,否则解包将不唯一
*a,b,*c = range(5) # wrong
*a,b,c = range(5) # right
a,*b,c = range(5) # right
  1. 为了收集“其余”元素,加星号的表达式必须与强制目标一起使用。尾随逗号用于指示强制目标不存在
*a = range(5) # wrong
*a, = range(5) # right

我相信如果你掌握了这两个规则,你可以推断出左侧星号表达式的任何结果。

The original idea of starred expression at the lhs is to improve the readability of iterable unpacking as below :

first_param, rest_param, third_param = param[0], param[1:-1], param[-1]

This statment is equvailent to

first_param, *rest_param, third_param = param

In the statement above, starred expression is used to 'catch' all elements that are not assigned to 'mandatory targets' (first_param & third_param in this example)

The use of starred expression at the lhs has the following rules:

  1. at most one starred expression at the lhs, or the unpacking will not be unique
*a,b,*c = range(5) # wrong
*a,b,c = range(5) # right
a,*b,c = range(5) # right
  1. In order to collect 'rest' elements, starred expression must be used with mandatory targets. Trailing comma is used to indicate mandatory targets do not exist
*a = range(5) # wrong
*a, = range(5) # right

I belive if you master these two rules, you can deduce any result of starred expression at the lhs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文