【发布时间】:2022-11-06 02:59:52
【问题描述】:
recordclass 数据对象可以很好地处理枚举属性,除非您需要设置默认值,这会导致 SyntaxError(从版本 0.17.5 开始):
In [1]: from enum import Enum, auto
In [2]: from recordclass import dataobject
In [3]: class Color(Enum):
...: RED = auto()
...:
In [4]: class Point(dataobject):
...: x: float
...: y: float
...: color: Color
...:
In [5]: pt = Point(1, 2, Color.RED)
In [6]: pt
Out[6]: Point(x=1, y=2, color=<Color.RED: 1>)
In [7]: class Point(dataobject):
...: x: float
...: y: float
...: color: Color = Color.RED
...:
...:
Traceback (most recent call last):
...
File "<string>", line 2
def __new__(_cls_, x, y, color=<Color.RED: 1>):
^
SyntaxError: invalid syntax
这个问题有解决方法吗?
【问题讨论】:
-
<Color.RED: 1>不是有效的 Python 语法。我想你想要Color.RED。这实际上与默认参数无关。
标签: python enums recordclass