调用不存在的非

2024-04-26 10:03:27 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我将一个异常命名为变量,那么该变量拥有哪些属性?例如

try:
    None.nonexistent_function()
    #return an AttributeError.
except Exception as ex:
    self.assertEqual(__, ex.__class__.__name__)

在这种情况下,有什么必要使断言成为真的呢?如何确定异常的名称和类?在

这个问题是Python-Koans的一部分,Python-Koans是Ruby-Koans的最新端口。


Tags: selfnoneanreturn属性asexceptionfunction
2条回答

嗯。。。使用Python的命令行,我得到:

>>> try:
...     None.nonexistent_function()
...     #return an AttributeError.
... except Exception as ex:
...     print ex.__class__.__name__
... 
AttributeError
>>> 

所以让我们试试:

^{pr2}$

我没有你的self对象方便的任何东西,所以你必须测试其余的。这对你有用吗?在

在REPL上试试:

>>> try: None.foo()
... except Exception as ex: pass
...
>>> # ex is still in scope, so we can play around with it and find out for ourselves:
... ex.__class__.__name__
'AttributeError'
>>>

相关问题 更多 >