python3.8 := and python3.7 dataclass
代码示例
from dataclasses import field,dataclass
@dataclass
class People:
name :str =field(init="张三")
mylist: list = field(default_factory=list)
age :int = 26
def run(self):
self.mylist.append(self.age)
if (n:=len(self.mylist))>0:
print(f"元素个数为:{n}")
print(self.name)
print(self.mylist)
if __name__ == '__main__':
p=People(name="哈哈")
p.run()