擅长:python、mysql、java
<p><a href="https://docs.python.org/3/library/dataclasses.html" rel="nofollow noreferrer">dataclasses.py</a>是一个<strong>模块,它通过使用字段注释为生成常规类方法提供装饰符和函数。这意味着在处理类之后,用户定义的字段应使用<a href="https://www.python.org/dev/peps/pep-0526/#runtime-effects-of-type-annotations" rel="nofollow noreferrer">PEP 526 Syntax of Variable annotations</a>形成。模块注释可以作为<code>__annotations__</code>访问。</p>
<p>根据<a href="https://www.python.org/dev/peps/pep-0526/#runtime-effects-of-type-annotations" rel="nofollow noreferrer">Runtime effects of type annotations</a>,可以通过<code>__annotations__</code>属性或使用<a href="https://docs.python.org/3/library/typing.html#typing.get_type_hints" rel="nofollow noreferrer">typing.get_type_hints</a>(最后一个是推荐的)来访问带注释的类型。</p>
<p>请参阅下面的一些代码示例:</p>
<pre><code>from typing import Dict, ClassVar, get_type_hints
from dataclasses import dataclass
@dataclass
class Starship:
hitpoints: int = 50
get_type_hints(Starship) // {'hitpoints': int}
Starship.__annotations__ // {'hitpoints': int}
dataclasses.__annotations__ // The annotations of the dataclasses module.
get_type_hints(get_type_hints)
</code></pre>