相关疑难解决方法(0)

关于`namedtuple`的一些内容在3.5.1中有所改变吗?

在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()?).

这是故意的吗?我们不应该使用这种模式将命名元组转换为字典吗?

python typeerror namedtuple python-3.5

34
推荐指数
3
解决办法
3062
查看次数

标签 统计

namedtuple ×1

python ×1

python-3.5 ×1

typeerror ×1