详见官方文档:https://peps.python.org/pep-0557/
dataclass 可以方便类的定义。
举个简单的例子:
from dataclasses import dataclass
@dataclass
class Person:
name: str = ""
age: int = 20
weight: float =20.0
相当于=
class OldPerson:
def __init__(self, name="Mark", age=10, weight=20.0)
self.name = name
self.age = age
self.weight = weight
def __repr__(self):
return f"Person(name={self.name}, age={self.age}, weight={self.weight})"