- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
在 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)])
在 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
似乎关于 namedtuple
的某些内容发生了变化(或者可能是关于 vars()
的某些内容?)。
这是故意的吗?难道我们不应该再使用这种模式将命名元组转换为字典了吗?
最佳答案
根据 Python bug #24931 :
[
__dict__
] disappeared because it was fundamentally broken in Python 3, so it had to be removed. Providing__dict__
broke subclassing and produced odd behaviors.
具体来说,没有定义 __slots__
的子类会表现得很奇怪:
>>> Cluster = namedtuple('Cluster', 'x y')
>>> class Cluster2(Cluster):
pass
>>> vars(Cluster(1,2))
OrderedDict([('x', 1), ('y', 2)])
>>> vars(Cluster2(1,2))
{}
使用 ._asdict()
.
关于python - `namedtuple` 在 3.5.1 中有什么变化吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34166469/
typing module documentation表示下面的两个代码片段是等价的。 from typing import NamedTuple class Employee(NamedTuple)
是否可以在另一个 namedtuple 中包含一个 namedtuple? 例如: from collections import namedtuple Position = namedtuple('
为什么我不能 pickle typing.NamedTuple 而我可以 pickle collections.namedtuple?我如何设法 pickle NamedTuple? 这段代码展示了我
我正在尝试创建一个 NamedTuple,其中一个字段默认为空字典。这大部分都有效,但是默认值在 NamedTuple 的实例之间共享: from typing import NamedTuple,
我正在寻找一种类似结构的数据结构,我可以从中创建多个实例并具有某种类型提示而不是不可变的。 所以我有这样的东西: class ConnectionConfig(NamedTuple): nam
在下面的代码中: point=namedtuple('point','x y') 我很困惑,一个类被视为一个值。就像我做的那样: class point: ... 它在命名空间中创建一个名为
考虑以下扩展 list 并附加 orderedtuple 的类: from collections import namedtuple Order = namedtuple('Order', ['id
我一直在使用这个基本代码来构建一个简单的库存系统。我需要扩展它以跟踪离开库存的另一个零件列表。我想我可以通过为数据结构创建另一个命名元组来做到这一点。我遇到一些问题,因为两个命名元组需要存在并同时访问
Python namedtuple 工厂函数允许它创建的子类的名称被指定两次——第一次在声明的左侧,然后作为函数的第一个参数(IPython 1.0.0 , Python 3.3.1): In [1]
最近我有一个关于数据类型的问题。 从那时起,我一直在尝试使用 NamedTuples(或多或少取得了成功)。 我目前的问题: - 如何将行从文件导入到新元组, - 如何将以空格/制表符(/无论什么)分
我有一个类,它几乎没有静态字段,并且是从可迭代对象初始化的(比如 csvreader 的输出)。 __init__ 为其中一些执行从字符串到数字的类型转换: class PerformanceTest
我知道 collections.namedtuple 的存在和目的,但我注意到,至少在 IDLE (3.2.2) 中,这个工厂函数也在 functools: >>> import functools
我想存储一个维度 namedtuple (x, y)。在我的整个程序中我只需要一次。 我能做到: Dimension = namedtuple('Dimension', ['x', 'y']) dim
有没有办法为 python 中的 namedtuple 重载相等运算符 __eq__(self, other)? 我知道这在类和重新定义方法中是可能的,但这对于 namedtuple 是否也可能,您将
这个问题在这里已经有了答案: Why should I use CamelCase for namedtuple? (1 个回答) 关闭 3 年前。 我是 Python 的新手,我一直在阅读在线文档
我们像这样使用 namedtuple: >>> from collections import namedtuple >>> Point = namedtuple('Point', ['x', 'y'
我正在处理命名元组列表。我想在创建每个命名元组后为其添加一个字段。似乎我可以通过将它作为属性引用来做到这一点(如 namedtuple.attribute = 'foo'),但它不会添加到字段列表中。
我正在尝试记录 namedtuple。当我构建文档时,我收到警告 WARNING: duplicate object description 和在记录的函数之后相同的空函数。例如: 如何删除那些别名?
在 Python 3.6 之后,我们有 typing.NamedTuple ,它是 collections.namedtuple() 的类型化版本,我们可以像类一样继承它: class Employe
鉴于我想从类型模块中正确使用命名元组的类型注释: from typing import NamedTuple, List class Foo(NamedTuple): my_list: Lis
我是一名优秀的程序员,十分优秀!