考虑到我有单个元素可迭代,例如元组
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有什么建议吗?