擅长:python、mysql、java
<p>简单的回答是,<a href="https://www.python.org/dev/peps/pep-0557/" rel="nofollow noreferrer">PEP 557</a>涵盖了这一切。你的问题有点乱。。。在</p>
<h3>为什么?</h3>
<ol>
<li>利用pep526提供一种简单的方法来定义这些类。在</li>
<li>支持静态类型检查程序。在</li>
</ol>
<h3>如何选择何时使用它们?</h3>
<p>政治公众人物很清楚,他们不是替代者,并期望其他解决方案有自己的位置。在</p>
<p>与其他任何设计决策一样,您需要确切地决定您所关心的特性。如果这包括以下内容,那么您肯定不需要dataclasses。在</p>
^{bq}$
<p>也就是说,SimpleNameSpace也是如此,那么我们还可以看什么来决定呢?让我们仔细看看dataclasses提供的额外特性。。。在</p>
<p>SimpleNameSpace的现有定义如下:</p>
<blockquote>
<p>A simple object subclass that provides attribute access to its namespace, as well as a meaningful repr.</p>
</blockquote>
<p>python文档接着说它提供了一个简单的<code>__init__</code>、<code>__repr__</code>和{<cd3>}实现。与PEP 557相比,dataclasses还提供了以下选项:</p>
<ul>
<li>排序-将类作为其字段的元组按顺序进行比较。在</li>
<li>不变性-分配给字段将生成异常</li>
<li>控制散列-尽管不建议这样做。在</li>
</ul>
<p>显然,如果您关心排序或不变性(或者需要小生境散列控件),您应该使用dataclasses。在</p>
<h3>其他用例?</h3>
<p>我看不到,尽管你可以说,最初的“为什么?”涵盖其他用例。在</p>