Python Dict 与 Asdict


Python 3.7引入了dataclasses库,让我们可以制作专门用于数据存储的结构化类。 这些类具有处理数据及其描述的特定属性和方法。


Python 中的数据类库

要安装数据类库,请使用以下命令。

pip install dataclasses

与 Python 中的普通类不同,数据类是使用带有类的 @dataclass 装饰器实现的。 此外,属性声明是使用类型提示进行的,类型提示为数据类中的属性指定数据类型。

下面是将概念付诸实践的代码片段。

# A bare-bones Data Class
# Don't forget to import the dataclass module
from dataclasses import dataclass
@dataclass
class Student():
    """A class which holds a students data"""

    # Declaring attributes
    # Making use of type hints

    name: str
    id: int
    section: str
    classname: str
    fatherName: str
    motherName: str

# Below is a dataclass instance
student = Student("Muhammad", 1432, "Red", "0-1", "Ali", "Marie")
print(student)

输出:

Student(name='Muhammad', id=1432, section='Red', classname='0-1', fatherName='Ali', motherName='Marie')

上面的代码有两点需要注意。 首先,数据类对象接受参数并在没有 _init_() 构造函数的情况下将它们分配给相关数据成员。

这是因为数据类提供了一个内置的 _init_() 构造函数。

第二点要注意的是,print 语句巧妙地打印了对象中存在的数据,而没有专门为此编写的任何函数。 这意味着它必须有一个改变的 repr() 函数。


为什么 dict 比 asdict 快

在大多数情况下,如果您在没有数据类的情况下使用 dict,您当然应该继续使用 dict。

但是,asdict 在复制调用期间执行额外的任务,这可能对您的情况没有用。 这些额外的任务会产生您想要避免的开销。

根据官方文档,这是它的作用。 每个数据类对象首先转换为其字段的字典作为名称:值对。

然后,递归数据类、字典、列表和元组。

例如,如果您需要递归数据类听写,请使用 asdict。 否则,提供它的所有额外工作都被浪费了。

如果您特别使用 asdict,则修改包含对象的实现以使用数据类将更改 asdict 在外部对象上的结果。

from dataclasses import dataclass, asdict
from typing import List
@dataclass
class APoint:
     x1: int
     y1: int
@dataclass
class C:
     aList: List[APoint]
point_instance = APoint(10, 20)
assert asdict(point_instance) == {'x1': 10, 'y1': 20}
c = C([APoint(30, 40), APoint(50, 60)])
assert asdict(c) == {'aList': [{'x1': 30, 'y1': 40}, {'x1': 50, 'y1': 60}]}

此外,递归业务逻辑无法处理循环引用。 如果你使用数据类来表示,比方说,一个图形,或者其他一些具有循环引用的数据结构,asdict 肯定会崩溃。

@dataclasses.dataclass
class GraphNode:
    name: str
    neighbors: list['GraphNode']
x = GraphNode('x', [])
y = GraphNode('y', [])
x.neighbors.append(y)
y.neighbors.append(x)
dataclasses.asdict(x)
# The code will crash here as
# the max allowed recursion depth would have exceeded
# while calling the python object
# in case you're running this on jupyter notebook notice
# that the kernel will restart as the code crashed

此外,asdict 构建了一个新的字典,__dict__ 虽然直接访问了对象的字典属性。

重要的是要注意 asdict 的返回值无论如何都不会受到原始对象属性重新分配的影响。

此外,考虑到如果您将属性添加到未映射到已声明字段的数据类对象,则 asdict 使用字段,asdict 将不会包含它们。

最后,尽管文档没有明确提及,但 asdict 将对任何非数据类实例、字典、列表或元组的内容调用深度复制。

return copy.deepcopy(instance) # a very costly operation !

数据类实例、dict、列表和元组通过递归逻辑,它另外构建了一个副本,只是应用了递归听写。

如果您相当精通面向对象的范例,那么您就会知道深拷贝本身就是一项代价高昂的操作,因为它会检查每个对象以查看需要复制的内容; 缺少备忘录处理本质上意味着 asdict 很可能会在非平凡的对象图中创建共享对象的多个副本。

当心这样的场景:

from dataclasses import dataclass, asdict
@dataclass
class PointClass:
    x1: object
    y1: object
obj_instance = object()
var1 = PointClass(obj_instance, obj_instance)
var2 = asdict(var1)
print(var1.x1 is var1.y1) # prints true
print(var2['x1'] is var2['y1']) # prints false
print(var2['x1'] is var1.x1) # prints false

输出:

True
False
False
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迹忆客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值