繁体   English   中英

Python覆盖对象中的回溯调用

[英]Python overwrite traceback call in objects

我有一个深层嵌套的同一类对象,我想检测回溯属性错误,所以我可以打印(“此级别没有属性”)消息,而不是显示回溯属性错误消息。

您可以在此处使用try-except子句:

try:
    #Your code here (Whatever thing gets to an attribute error)
except AttributeError:
    print("there is no attribute in this level")

使用getattr魔法方法。

def __getattr__(self, name):
           print("there is no attribute in this level")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM