Unpacking, extended unpacking and nested extended unpacking

前端 未结 3 1345
轮回少年
轮回少年 2020-11-22 16:40

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

(this is a long list)

a, b = 1, 2               


        
3条回答
  •  Happy的楠姐
    2020-11-22 17:33

    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.

提交回复
热议问题