有什么办法可以获取有关 Python 中 AttributeError 异常的具体详细信息吗?
我正在尝试调用一个函数。其中一个参数是一个带有属性的变量(我知道这是因为我得到了 AttributeError 异常)。我不知道这个变量应该具有的确切属性,所以我想知道是否有某种方法可以看到有关异常的一些额外详细信息,例如,它找不到哪个属性。谢谢。
I'm trying to call a function. One of the parameters is a variable with attributes (which I know because of the AttributeError exception I got). I don't know the exact attributes this variable is supposed to have, so I was wondering if there was some way I can see some extra details about the exception, for example, which attribute it couldn't find. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
AttributeError
通常标识丢失的属性。例如:当我运行它时,我看到:
这非常明确。如果您没有看到类似的内容,请发布您所看到的确切错误。
编辑
如果您需要强制打印异常(无论出于何种原因),您可以执行以下操作:
这应该为您提供与异常相关的完整错误消息和回溯。
AttributeError
typically identifies the missing attribute. e.g.:When I run that, I see:
That's pretty explicit. If you're not seeing something like that, please post the exact error you're seeing.
EDIT
If you need to force the printing of an exception (for whatever reason), you can do this:
That should give you the full error message and traceback associated with the exception.
回溯应该提醒您引发
AttributeError
异常:或者,将
Exception
转换为str
:如果您想获取对象上可用属性的列表,请尝试
dir()
或help()
你甚至可以在
dir
上调用help()
(为什么留给读者作为练习):失败了这些......你可以随时查看代码,除非第三方向您提供了一些预编译模块,在这种情况下,您应该要求供应商提供更好的文档(比如一些单元测试!)!
The traceback should alert you to the attribute access that raised the
AttributeError
exception:Alternatively, convert the
Exception
tostr
:If you want to get a list of the attributes available on an object, try
dir()
orhelp()
You can even call
help()
ondir
(why is left as an exercise for the reader):Failing these... you could always look at the code, unless you've been provided some precompiled module by a third-party, in which case you should demand better documentation (say some unit tests!) from your supplier!
通常
AttributeError
带有一些与此相关的信息:Usually
AttributeError
carries some information about this with it: