【发布时间】:2016-07-17 00:31:49
【问题描述】:
我正在尝试将 python class 重构为 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
我现在的代码看起来像(在 Genie 中):
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''' 命令的指针。
【问题讨论】:
标签: genie