相关疑难解决方法(0)

有没有办法使用数据类,带有默认值的字段,带有 __slots__

我想放置__slots__一个带有默认字段的数据类。当我尝试这样做时,我收到此错误:

>>> @dataclass
... class C:
...     __slots__ = ('x', 'y', )
...     x: int
...     y: int = 1
...     
Traceback (most recent call last):
  File "<input>", line 1, in <module>
ValueError: 'y' in __slots__ conflicts with class variable
Run Code Online (Sandbox Code Playgroud)

有没有办法实现这一目标?

python python-dataclasses

4
推荐指数
1
解决办法
1047
查看次数

标签 统计

python ×1

python-dataclasses ×1