首页 > 解决方案 > 从 AttributeError 获取类和属性名称

问题描述

如何从 AttributeError 获取类和缺少的属性,例如,当 AttributeError 说:'NoneType' object has not attribute a,我想获取"NoneType""a"

标签: python

解决方案


看起来您可以从 an 中检索的唯一内容AttributeError是带有错误消息的字符串:

try:
    str.s
except AttributeError as err:
    error_message, = err.args
    print("Error message:", error_message)
    raise

推荐阅读