Python 3.7 提供了一个装饰器 dataclass,用于将类转换为 dataclass。
无需定义 init,然后将值赋给 self,dataclass 负责处理它
from typing import List
@dataclass
class C:
x: int
y: List[int]
t: int = 20 #被赋值默认值的对象一直在最后面(指定了类型)
class B:
x = 0
def __init__(self):
self.x = 200
@dataclass
class test:
x = 0#被赋值了默认是,但是没有指定类型,可以不用放到所有变量最后面
y:int
if __name__ == '__main__':
t = test(y=10)
print(t)
b = B()
print(b)
a = [1, 2, 3]
c = C(x=1, y=a)
print(c)
