- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Field 的文档python 标准类 dataclasses模块仅指定:
Its documented attributes are:
- [...]
- type: The type of the field.
对我来说,这似乎意味着字段将包含类型本身,而不仅仅是字符串形式的名称。
但是,它似乎只是简单地照原样复制了类型注释,这使得它毫无用处。
例子:
@dataclasses.dataclass
class C:
c: 'C'
dataclasses.fields(C)[0].type # This returns the string 'C'
typing.get_type_hints(C)['c'] # This returns the class C, as expected
问题甚至在使用PEP563 时系统地发生类型注解。
这是数据类模块中的错误吗?这是预期的行为吗?如果是这样,我如何在给定 Field 实例的情况下检索类型对象?
最佳答案
这是故意的。在导入时解析类型提示代价高昂,尤其是当 from __future__ import annotations
一开始就禁用解析它们时。
最初,当您使用 from __future__ import annotations
开关并包含 ClassVar
时,将 PEP 563 添加到 Python 3.7 会破坏数据类。或 InitVar
字段的类型注释;这些不会在这一点上得到解决,仍然是一个字符串。如果您明确使用字符串,这在 PEP 563 之前就已经是一个问题,请参阅 dataclasses issue #92 .这变成了 Python bug, #33453 ,一旦数据类将其纳入 Python 3.7 中。
“父”项目 attrs
启发了 dataclasses
,also had this issue to solve .在那里,Łukasz Langa(大多数类型提示 peps 的合著者,包括 PEP 563)指出:
OK, so I tried the above and it seems it's a nuclear option since it forces all annotations to be evaluated. This is what I wanted to avoid with
from __future__ import annotations
.
并且在 discussion on the pull request that fixed issue 33453 dataclasses
的作者 Eric Smith 表示:
I've been researching doing just that. I think @ambv's point is that it introduces a performance hit due to calling eval on every field, while the point of string annotations is to remove a performance hit.
此外,还有其他问题;您不能在导入时评估所有类型提示,而不是在它们使用前向引用时:
In addition to the performance issue, in the following case (without a
__future__
statement and without dataclasses), I get an error onget_type_hints()
becauseC
is undefined whenget_type_hints()
is called. This is python/typing#508. Notice that whereget_type_hints()
is called in this example is exactly where@dataclass
is going to run and would need to call the stripped downget_type_hints()
.
所以最后,dataclasses
所做的只是将字符串试探法应用于注释,而不会为您加载它们。
要检索类型,只需在类本身上使用 get_type_hints()
,并将字段 .name
属性作为结果的键:
resolved = typing.get_type_hints(C)
f = dataclasses.fields(C)[0]
ftype = resolved[f.name]
关于python - dataclasses.Field 不将类型注释解析为实际类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55937859/
c1 和c2 的构造函数中的默认值应该为b 和b 生成新的实例变量。相反,它看起来像 c1.a 和 c2.a 引用了同一个变量。 @dataclass 是否正在创建类变量?这似乎与预期的功能不一致,而
这个问题已经有答案了: C# - Cannot implicitly convert type List to List (6 个回答) Why does this generic cast fail
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 去年关闭。 Improve th
如何让它在 @dataclass 修饰的 Python 类中设置拼写错误的字段时引发异常? 我想要一个实用的方法来做到这一点。我是否需要改为编写自己的装饰器? @dataclass class C(o
Field 的文档python 标准类 dataclasses模块仅指定: Its documented attributes are: [...] type: The type of the fie
我正在尝试比较从公共(public)基类(也是@dataclass)继承的两个数据类。 继承类的字段是自己特有的,比较时不考虑;我只想比较基类属性。 这是我的尝试: from dataclasses
我试图在整个应用程序中使用整数和文本。我是通过使用 DataClass 来这样做的,并且我已经得到了可以工作的整数。但是,我不确定如何使用文本。我很确定使用 UILabel 是不正确的。我将在下面向您
我正在使用来自 dataclasses 的 dataclass 和 asdict ,我发现 asdict 并不像我在引入继承时所期望的那样工作。 我使用 dataclasses 来帮助我从类创建字典,
Python 3.7 引入了包含 @dataclass 装饰器的 dataclasses 模块。这个装饰器可以生成类函数。如何打印这些生成的函数? 最佳答案 我自己也问过同样的问题。 Dataclas
我有一堆@dataclass es 和一堆对应的 TypedDict s,我想促进它们之间的平滑和类型检查转换。 例如,考虑 from dataclasses import dataclass fro
我已经使用 highchart 绘制了一个热图,但我想为 y 轴提供颜色范围,即数据数组中的第二个索引,但它会自动采用第三个索引。 下面是jsfiddle代码。 Links for my code
Python 3.7 引入 dataclasses来存储数据。我正在考虑采用这种新方法,这种方法比字典更有条理,结构更合理。 但我有一个疑问。 Python 将键转换为 dicts 上的哈希,这使得查
在下面的代码中,astuple 函数正在对数据类的类属性进行深度复制。为什么它不会产生与函数 my_tuple 相同的结果? import copy import dataclasses @datac
总结 我有一个包含 10 多个字段 的数据类。 print()将它们隐藏在默认值墙中的有趣上下文 - 让我们通过避免不必要地重复这些来使它们更友好。 Python 中的数据类 Python 的 @da
我有一个数据类,它看起来像这样: @dataclass class myClass: id: str mode: str value: float 这导致: dataclasses.
我有一个类,我们称它为 Builder。它有一个私有(private)成员声明为 vector> buffers; // Node is a pure data class. 在这个 Builder
Python 3.7 提供了新的 dataclasses,它们具有预定义的特殊功能。 从总体上看,dataclasses 和 SimpleNamespace 都提供了很好的数据封装工具。 @datac
我试图在我的单元测试中使用 freezegun 来修补数据类中的一个字段,该字段设置为对象初始化时的当前日期。我想这个问题与任何尝试修补一个在 freezegun 之外用作 default_facto
我想使 set 参数可选,但仍允许 None 为有效值。基于the documentation , 它建议 dataclasses.MISSING 可以使用默认值来帮助实现这一点。 As shown
我想使 set 参数可选,但仍允许 None 为有效值。基于the documentation , 它建议 dataclasses.MISSING 可以使用默认值来帮助实现这一点。 As shown
我是一名优秀的程序员,十分优秀!