在Python 3.5.0上:
>>> from collections import namedtuple
>>> cluster = namedtuple('Cluster', ['a', 'b'])
>>> c = cluster(a=4, b=9)
>>> c
Cluster(a=4, b=9)
>>> vars(c)
OrderedDict([('a', 4), ('b', 9)])
Run Code Online (Sandbox Code Playgroud)
在Python 3.5.1上:
>>> from collections import namedtuple
>>> cluster = namedtuple('Cluster', ['a', 'b'])
>>> c = cluster(a=4, b=9)
>>> c
Cluster(a=4, b=9)
>>> vars(c)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: vars() argument must have __dict__ attribute
Run Code Online (Sandbox Code Playgroud)
似乎有些事情发生了namedtuple变化(或者可能是某些事情vars()?).
这是故意的吗?我们不应该使用这种模式将命名元组转换为字典吗?