Python 3.7:数据类和 SimpleNameSpace 的实用程序 [英] Python 3.7: Utility of Dataclasses and SimpleNameSpace

查看:104
本文介绍了Python 3.7:数据类和 SimpleNameSpace 的实用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 3.7 提供了具有预定义特殊功能的新数据类.

Python 3.7 provides new dataclasses which have predefined special functions.

从概述的角度来看,dataclassesSimpleNameSpace 都提供了很好的数据封装工具.

From an overview point, dataclasses and SimpleNameSpace both provide nice data encapsulating facility.

@dataclass
class MyData:
    name:str
    age: int

data_1 = MyData(name = 'JohnDoe' , age = 23)

data_2 = SimpleNameSpace(name = 'JohnDoe' , age = 23)

很多时候我使用 SimpleNameSpace 只是为了包装数据并移动它.

A lot of times I use SimpleNameSpace just to wrap data and move it around.

我什至子类化它以添加特殊功能:

I even subclass it to add special functions:

from types import SimpleNameSpace

class NewSimpleNameSpace(SimpleNameSpace):
    def __hash__(self):
        return some_hashing_func(self.__dict__)

对于我的问题:

  1. 人们如何在SimpleNameSpacedataclasses 之间做出选择?
  2. 为什么它们是必要的,当通过扩展 SimpleNameSpace 可以达到相同的效果时?
  3. dataclasses 的所有其他用例适用于哪些情况?
  1. How does someone choose between SimpleNameSpace and dataclasses?
  2. Why were they necessary, when the same effect can be achieved with extending the SimpleNameSpace?
  3. What all other use cases dataclasses cater to?

推荐答案

简短的回答是,PEP 557.你的问题有点乱...

The short answer is that this is all covered by PEP 557. Taking your questions slightly out of order...

  1. 利用 PEP 526 提供定义此类类的简单方法.
  2. 支持静态类型检查器.

如何选择何时使用它们?

PEP 非常清楚,它们不是替代品,并希望其他解决方案拥有自己的位置.

How to pick when to use them?

The PEP is quite clear that they are not a replacement and expect the other solutions to have their own place.

与任何其他设计决策一样,因此您需要准确确定您关心的功能.如果包括以下内容,您肯定不需要数据类.

Like any other design decision, you'll therefore need to decide exactly what features you care about. If that includes the following, you definitely don't want dataclasses.

哪些地方不适合使用数据类?

Where is it not appropriate to use Data Classes?

需要 API 与元组或字典的兼容性.需要进行超出 PEP 484 和 526 提供的类型验证,或者需要进行值验证或转换.

API compatibility with tuples or dicts is required. Type validation beyond that provided by PEPs 484 and 526 is required, or value validation or conversion is required.

也就是说,对于 SimpleNameSpace 也是如此,那么我们还能看什么来决定呢?让我们仔细看看数据类提供的额外功能...

That said, the same is true for SimpleNameSpace, so what else can we look at to decide? Let's take a closer look at the extra features provided by dataclasses...

SimpleNameSpace 的现有定义如下:

The existing definition of SimpleNameSpace is as follows:

一个简单的对象子类,提供对其命名空间的属性访问,以及一个有意义的代表.

A simple object subclass that provides attribute access to its namespace, as well as a meaningful repr.

python 文档接着说它提供了一个简单的 __init____repr____eq__ 实现.与 PEP 557 相比,数据类还为您提供以下选项:

The python docs then go on to say that it provides a simple __init__, __repr__ and __eq__ implementation. Comparing this to PEP 557, dataclasses also give you options for:

  • 排序 - 按顺序比较类,就好像它是其字段的元组一样.
  • 不变性 - 分配给字段会产生异常
  • 控制散列 - 尽管不推荐这样做.

显然,如果您关心排序或不变性(或需要小众哈希控制),则应该使用数据类.

Clearly, then, you should use dataclasses if you care about ordering or immutability (or need the niche hashing control).

我看不到,尽管您可以争辩说最初的为什么?"涵盖其他用例.

None that I can see, though you could argue that the initial "why?" covers other use cases.

这篇关于Python 3.7:数据类和 SimpleNameSpace 的实用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆