Star * operator on left vs right side of an assignment statement

a 夏天 提交于 2019-11-27 15:07:46

The difference between these two cases are explained when also taking into consideration the initial PEP for extended unpacking: PEP 3132 -- Extended iterable unpacking.

In the Abstract for that PEP we can see that:

This PEP proposes a change to iterable unpacking syntax, allowing to specify a "catch-all" name which will be assigned a list of all items not assigned to a "regular" name.

(emphasis mine)

So in the first case, after executing:

*elements, = iterable

elements is always going to be a list containing all the items in the iterable.

Even though it seems similar in both cases, the * in this case (left-side) means: catch everything that isn't assigned to a name and assign it to the starred expression. It works in a similar fashion as *args and **kwargs do in function definitions.

def spam(*args, **kwargs): 
    """ args and kwargs group positional and keywords respectively """

The second case (right-side) is somewhat different. Here we don't have the * working in a "catch everything" way as much as we have it working as it usually does in function calls. It expands the contents of the iterable it is attached to. So, the statement:

elements = *iterable, 

can be viewed as:

elements = 1, 2, 3, 4, 

which is another way for a tuple to be initialized.

Do note, a list can be created by simple using elements = [*iterable] which will unpack the contents of iterable in [] and result in an assignments of the form elements = [1, 2, 3, 4].

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!