在Python中解包单个可迭代元素的首选方法

时间:2018-08-30 12:19:49

标签: python

考虑到我有单个元素可迭代,例如元组

t = (1,)

是否存在将元组解包为变量的首选方法?有很多选择,各有利弊。

unpacked, = t    #  Elegant but easy to overlook.
(unpacked,) = t  #  Maybe a bit clunky?
unpacked, *_ = t #  Creates the impression, that there might be more elements.
unpacked = t[0]  #  Same as above.
*_, unpacked = t #  Same as above, but also gives the impression
                 #  that we are accessing the last element.
[unpacked] = t   #  Elegant and obvious.

PEP8有什么建议吗?

0 个答案:

没有答案