我正在尝试将一个python 类重构为 Genie,但我对如何处理错误感到困惑。一些指针将不胜感激。
如果我理解正确的话,使用 Genie 处理错误的方法是使用 Try...except 块,但是如何将以下类型的错误处理转换为这种范例:
# Enable dictionary/list-style access to options and arguments.
def __getitem__(self, key):
if isinstance(key, int):
if key < len(self.arguments):
return self.arguments[key]
else:
raise ArgParserError(
"positional argument index [%s] is out of bounds" % key
)
else:
option = self._get_opt(key)
return option.value
我现在的代码看起来像(在精灵中):
def getitem (key:int):string
if key.is_int()
if key < len(_arguments)
return _arguments[key]
else
raise ArgParserError(
"positional argument index [%s] is out of bounds", key
)
else
var option = _get_opt(key)
return option.value
这是一个虚拟代码,我只是对问题进行建模,我知道它不会按原样编译。我只是在寻找有关如何从 python 转换 '''raise''' 命令的指针。