2
我想指定(作爲類型提示)當前定義的類型作爲方法的返回類型。(Python類型提示)如何指定當前定義的類型作爲方法的返回類型?
下面是一個例子:
class Action(Enum):
ignore = 0
replace = 1
delete = 2
@classmethod
# I would like something like
# def idToObj(cls, elmId: int)->Action:
# but I cannot specify Action as the return type
# since it would generate the error
# NameError: name 'Action' is not defined
def idToObj(cls, elmId: int):
if not hasattr(cls, '_idToObjDict'):
cls._idToObjDict = {}
for elm in list(cls):
cls._idToObjDict[elm.value] = elm
return cls._idToObjDict[elmId]
理想我也喜歡到指定類似
def idToObj(cls, elmId: int)->Action:
謝謝。