简体   繁体   中英

Python overwrite traceback call in objects

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

You can use the try-except clause here:

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

Use the getattr magic method.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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