簡體   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