最佳答案
为了封装状态列表,我使用 enum
模块:
from enum import Enum
class MyEnum(Enum):
state1='state1'
state2 = 'state2'
state = MyEnum.state1
MyEnum['state1'] == state # here it works
'state1' == state # here it does not throw but returns False (fail!)
然而,问题在于我需要在脚本的许多上下文中无缝地使用这些值作为字符串,比如:
select_query1 = select(...).where(Process.status == str(MyEnum.state1)) # works but ugly
select_query2 = select(...).where(Process.status == MyEnum.state1) # throws exeption
如何避免调用额外的类型转换(上面的 str(state)
)或基础值(state.value
) ?