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()     
        

posted @ 2019-05-17 17:23  公众号python学习开发  阅读(429)  评论(0编辑  收藏  举报