python笔记:dataclass

1 引子:其他类似实现方法的局限性

假设我们现在需要实现这样的内容:

name China
area 960
population 140967

1.1 tuple/list

country1_tuple=('China','960','140967')
country1_tuple[0]
#'China'

缺点:需要记住各个属性是list/tuple第几位的属性(基于位置)

1.2 字典

country1_dict={'name':'China',
              'area':960,
              'population':140967}
country1_dict['name']
#'China'
  • 不足
    • 不能用country1_dict.name的形式进行控制
    • 少值【country1_dict={'name':'China', 'area':960】一样可以创建成功

1.3 namedtuple

python 库整理: collections.namedtuple_python collections.namedtuple创建一个-CSDN博客

from collections import namedtuple

Country=namedtuple('Country1',['name','area','population'])

country1=Country('China',960,'140967')
country2=Country('Singapore',733,564)

country1
#Country1(name='China', area=960, population='140967')

country2.name
#'Singapore'
  • 弥补了字典的两个缺点
    • 可以用country2.name的形式
    • 少值会报错
  • 不足
    • 无法修改值

1.4 自定义类

class Country:
    def __init__(self,name,area,population):
        self.name=name
        self.area=area
        self.population=population
        
country1_class=Country('China',960,140967)
country1_class.name
#'China'


country1_class.name="PRC"
  • 解决了namedtuple无法修改值的问题
  • 不足:
    • __init__方法中重复代码 (示例中每个属性都需要写3遍)

2 数据类dataclass

  • 提供了一个简写__init__方法的语法糖. 
  • 类型注释是必填项
  • 默认值的传递方式和__init__方法的参数格式一致
from dataclasses import dataclass

@dataclass
class Country_dc:
    name:str
    area:int
    population:int=40
        
country_dc1=Country_dc('China',960,140967)
country_dc2=Country_dc('Singapore',733)


country_dc1
#Country_dc(name='China', area=960, population=140967)


country_dc2
#Country_dc(name='Singapore', area=733, population=40)

2.1 数据嵌套

from typing import List

@dataclass
class Countrys_dc:
    name:str
    countries:List[Country_dc]
country_lst=Countrys_dc('China & SG',[country_dc1,country_dc2])
country_lst
'''
Countrys_dc(name='China & SG', countries=[Country_dc(name='China', area=960, population=140967), Country_dc(name='Singapore', area=733, population=40)])
'''

2.2 数据类不可变

 要使数据类不可变,需要在创建类时设置frozen=True。

参考内容:Python中的数据类dataclass详解_python dataclass-CSDN博客

相关推荐

  1. 译文带你理解Pythondataclass装饰器

    2024-05-05 04:04:02       34 阅读
  2. Python BaseModel和dataclass用法和区别

    2024-05-05 04:04:02       18 阅读
  3. python使用dataclass数据类有个坑

    2024-05-05 04:04:02       14 阅读
  4. pythonpython基础学习笔记

    2024-05-05 04:04:02       13 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-05 04:04:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-05 04:04:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-05 04:04:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-05 04:04:02       20 阅读

热门阅读

  1. 1.Spring Security介绍

    2024-05-05 04:04:02       23 阅读
  2. Vue框架知识点表格总结

    2024-05-05 04:04:02       21 阅读
  3. 使用Spring Boot快速构建Spring应用

    2024-05-05 04:04:02       11 阅读
  4. linux定时运行脚本

    2024-05-05 04:04:02       13 阅读
  5. Python ansible 如何使用

    2024-05-05 04:04:02       11 阅读
  6. github.com/gin-contrib/timeout应前置使用

    2024-05-05 04:04:02       10 阅读
  7. 如何在 MySQL 中创建新用户并授予权限

    2024-05-05 04:04:02       13 阅读
  8. Docker容器管理详解

    2024-05-05 04:04:02       48 阅读