Python 数据类 : What type to use if __post_init__ performs type conversion?

标签 python python-dataclasses

我有一个 Python 类,它的字段可以传递几种序列类型之一。为了简化,我将坚持使用元组和列表。 __init__将参数转换为 MyList .

from typing import Union
from dataclasses import dataclass, InitVar, field

class MyList(list):
    pass

@dataclass
class Struct:
    field: Union[tuple, list, MyList]

    def __post_init__(self):
        self.field = MyList(self.field)

我应该为 field 使用什么类型宣言?
  • 如果我提供所有可能的输入类型的联合,则代码不会记录 field始终是 MyList访问时。
  • 如果我只提供最终的 MyList类型,当我通过时 PyCharm 会提示 Struct() list .

  • 我可以改为使用:
    _field: InitVar[Union[tuple, list, MyList]] = None
    field: MyList = field(init=False)
    
    def __post_init__(self, _field):
        self.field = MyList(_field)
    

    但这非常难看,尤其是在 3 个字段中重复时。此外,我必须构建一个结构,如 Struct(_field=field)而不是 Struct(field=field) .

    2018 年 4 月,“tm”在 PyCharm 的公告中对此问题进行了评论:https://blog.jetbrains.com/pycharm/2018/04/python-37-introducing-data-class/#comment-323957

    最佳答案

    您正在将向属性分配值与生成要分配给属性的值的代码混为一谈。我会使用一个单独的类方法来保持两段代码分开。

    from dataclasses import dataclass
    
    
    class MyList(list):
        pass
    
    
    @dataclass
    class Struct:
        field: MyList
    
        @classmethod
        def from_iterable(cls, x):
            return cls(MyList(x))
    
    
    s1 = Struct(MyList([1,2,3]))
    s2 = Struct.from_iterable((4,5,6))
    
    现在,您只需传递一个现有值 MyListStruct.__init__ .元组、列表和其他任何东西 MyList能接受的都传给Struct.from_iterable相反,它将负责构建 MyList传递给 Struct 的实例.

    关于Python 数据类 : What type to use if __post_init__ performs type conversion?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51737828/

    相关文章:

    python - 使用 numpy 从过滤、排序的数组返回索引

    python - AttributeError,我在这里做错了什么?

    python - napoleon 和 autodoc 如何交互记录成员

    python - 验证 python 数据类中的详细类型

    Python:数据类/前向变量声明的循环依赖?

    python - Pandas.to_csv 给出错误 'ascii' codec can't encode character u'\u2013' in position 8 : ordinal not in range(128)

    python - 用ast重写代码; Python

    python - Django 发送电子邮件困惑

    python - 是否可以防止从卡住的 python 数据类中读取?

    Python:将类对象转换为 JSON - 对象不可 JSON 序列化