📜  Python 3.7 数据类终极指南

📅  最后修改于: 2022-05-13 01:54:50.682000             🧑  作者: Mango

Python 3.7 数据类终极指南

本文讨论Python 3.7 中的数据类,并提供Python 3.7 及更高版本中的数据类的介绍性指南。

数据类是Python 3.7 版本中引入的一个新概念。您不仅可以将数据类用作知识容器,还可以为您编写样板代码并简化创建类的过程,因为它带有一些免费实现的方法。数据类也可以被视为通常包含数据的类别,但不限于此。

基本数据类

以下方式展示了如何创建基本数据类:

1. 使用装饰器:它们是使用新的@dataclass装饰器创建的。

例子:

Python3
# import package
from dataclasses import dataclass
  
# make a dataclass with decorator
@dataclass
class DataClassGFG:
    Job: str
    Salary: float
  
# make an object with values required by dataclass
DataClassObject = DataClassGFG("Author",50000.00)
  
# view dataclass object
print(DataClassObject)


Python3
# import package
from dataclasses import make_dataclass
  
# make a dataclass with make_dataclass
  
DataClassGFG = make_dataclass("DataClassGFG",["Job","Salary"])
  
# make an object with values required by dataclass
DataClassObject = DataClassGFG("Author",50000.00)
  
# view dataclass object
print(DataClassObject)


Python3
# import package
from collections import namedtuple
  
# make a dataclass with namedtuple
  
DataClassGFG = namedtuple("DataClassGFG",["Job","Salary"])
  
# make an object with values required by dataclass
DataClassObject = DataClassGFG("Author",50000.00)
  
# view dataclass object
print(DataClassObject)


Python3
# import package
from dataclasses import dataclass
  
# make a dataclass with decorator
@dataclass(frozen=True)
class DataClassGFG:
    Job: str
    Salary: float
  
# make an object with values required by dataclass
DataClassObject = DataClassGFG("Author",50000.00)
  
# view dataclass object
print(DataClassObject)
  
# check immutable nature of class
DataClassObject.Job = "Writer"


Python3
# import package
from dataclasses import dataclass
from typing import List
  
# make a dataclass with decorator
@dataclass
class DataClassGFG:
    Job: str
    Salary: float
          
@dataclass
class GFGJobs:
    Jobs: List[DataClassGFG]
  
# make objects with values required by dataclass
DataClassObject1 = DataClassGFG("Author",50000.00)
DataClassObject2 = DataClassGFG("Writer",40000.00)
  
# view dataclass objects
print(DataClassObject1)
print(DataClassObject2)
  
# make an object of another dataclass
GFGJobsObject = GFGJobs([DataClassObject1,DataClassObject2])
  
# view dataclass object
print(GFGJobsObject)


Python3
# import package
from dataclasses import dataclass
  
# make a dataclass with decorator
@dataclass
class DataClassGFG:
    Job: str
    Salary: float
          
@dataclass
class SlotClassGFG:
    __slots__ = ["Job","Salary"]
    Job: str
    Salary: float
  
# make objects with values required by dataclass
DataClassObject1 = DataClassGFG("Author",50000.00)
DataClassObject2 = SlotClassGFG("Writer",40000.00)
  
# view dataclass objects
print(DataClassObject1)
print(DataClassObject2)
  
# view dataclass object slot
print(DataClassObject2.__slots__)


Python3
# import package
from dataclasses import dataclass
  
# make a dataclass with decorator
@dataclass
class DataClassGFG:
    Job: str
    Salary: float
          
@dataclass
class SubDataClassGFG(DataClassGFG):
    Standard: str = "Top"
    Salary: float = 100000.00
  
  
# make objects with values required by dataclass
DataClassObject1 = DataClassGFG("Author",100000.00)
DataClassObject2 = DataClassGFG("Author",50000.00)
  
# view dataclass objects
print(DataClassObject1)
print(DataClassObject2)
  
# make object with values required by subdataclass
SubDataClassObject = SubDataClassGFG("Author")
  
# view subdataclass object
print(SubDataClassObject)


输出:

2. 使用 make_dataclass():

例子:

蟒蛇3

# import package
from dataclasses import make_dataclass
  
# make a dataclass with make_dataclass
  
DataClassGFG = make_dataclass("DataClassGFG",["Job","Salary"])
  
# make an object with values required by dataclass
DataClassObject = DataClassGFG("Author",50000.00)
  
# view dataclass object
print(DataClassObject)

输出:

3. 使用namedtuple 使用来自集合的 namedtuple() 方法。与 make_dataclass() 类似。

例子:

蟒蛇3

# import package
from collections import namedtuple
  
# make a dataclass with namedtuple
  
DataClassGFG = namedtuple("DataClassGFG",["Job","Salary"])
  
# make an object with values required by dataclass
DataClassObject = DataClassGFG("Author",50000.00)
  
# view dataclass object
print(DataClassObject)

输出:

不可变数据类

我们之前看到的namedtuple 的定义特征之一是它是不可变的,即其字段的值可能永远不会改变。对于几种类型的数据类,创建不可变类通常是一个很好的主意,只需在创建它后简单地设置 freeze=True 即可实现这一点。

例子:

蟒蛇3

# import package
from dataclasses import dataclass
  
# make a dataclass with decorator
@dataclass(frozen=True)
class DataClassGFG:
    Job: str
    Salary: float
  
# make an object with values required by dataclass
DataClassObject = DataClassGFG("Author",50000.00)
  
# view dataclass object
print(DataClassObject)
  
# check immutable nature of class
DataClassObject.Job = "Writer"

输出:

灵活的数据类

我们已经讨论了一些创建数据类的方法,现在我们将研究一些更高级的特性,比如 @dataclass 装饰器的参数。在创建数据类时,向数据类添加参数为我们提供了更多控制。

例子:

蟒蛇3

# import package
from dataclasses import dataclass
from typing import List
  
# make a dataclass with decorator
@dataclass
class DataClassGFG:
    Job: str
    Salary: float
          
@dataclass
class GFGJobs:
    Jobs: List[DataClassGFG]
  
# make objects with values required by dataclass
DataClassObject1 = DataClassGFG("Author",50000.00)
DataClassObject2 = DataClassGFG("Writer",40000.00)
  
# view dataclass objects
print(DataClassObject1)
print(DataClassObject2)
  
# make an object of another dataclass
GFGJobsObject = GFGJobs([DataClassObject1,DataClassObject2])
  
# view dataclass object
print(GFGJobsObject)

输出:

优化数据类

为了优化数据类,我们使用槽。插槽通常用于使类更快并使用更少的内存。数据类没有任何明确的处理槽的语法,但传统的槽的制作方法也适用于数据类。

例子:

蟒蛇3

# import package
from dataclasses import dataclass
  
# make a dataclass with decorator
@dataclass
class DataClassGFG:
    Job: str
    Salary: float
          
@dataclass
class SlotClassGFG:
    __slots__ = ["Job","Salary"]
    Job: str
    Salary: float
  
# make objects with values required by dataclass
DataClassObject1 = DataClassGFG("Author",50000.00)
DataClassObject2 = SlotClassGFG("Writer",40000.00)
  
# view dataclass objects
print(DataClassObject1)
print(DataClassObject2)
  
# view dataclass object slot
print(DataClassObject2.__slots__)

输出:

遗产

我们可以通过创建数据类的子数据类来简单地使用继承的概念。

例子:

蟒蛇3

# import package
from dataclasses import dataclass
  
# make a dataclass with decorator
@dataclass
class DataClassGFG:
    Job: str
    Salary: float
          
@dataclass
class SubDataClassGFG(DataClassGFG):
    Standard: str = "Top"
    Salary: float = 100000.00
  
  
# make objects with values required by dataclass
DataClassObject1 = DataClassGFG("Author",100000.00)
DataClassObject2 = DataClassGFG("Author",50000.00)
  
# view dataclass objects
print(DataClassObject1)
print(DataClassObject2)
  
# make object with values required by subdataclass
SubDataClassObject = SubDataClassGFG("Author")
  
# view subdataclass object
print(SubDataClassObject)

输出: