关于解包运算符 ( )的Python 3 教程*一般说的是“列表或元组”,而错误使用的错误消息说需要一个“序列”:
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> def f(a, b):
... return a / b
...
>>> f(*1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: f() argument after * must be a sequence, not int
Python 3 的内置类型文档列出了以下序列类型:
- 序列类型 -
list,tuple,range - 文本序列类型 —
str - 二进制序列类型 -
bytes,bytearray,memoryview
快速测试:
>>> all(isinstance(x, collections.Sequence) for x in [[], (), range(1), '', b''])
True
请注意,此处不包括集合类型(如set和frozenset)和映射类型(dict) 。
>>> any(isinstance(x, collections.Sequence) for x in [set(), {}])
False
我的问题:为什么所有可迭代类型(包括 aset或dict)都是不可打包的?它们不是序列类型,正如TypeError上面所暗示的那样,当为位置参数解包时,无序行为会导致未定义的结果:
>>> def f(a, b):
... return a / b
...
>>> f(*{4, 2})
0.5
>>> f(*{8, 2})
4.0
>>> f(*{4:1, 2:1})
0.5
>>> f(*{2:1, 8:1})
4.0