if __name__ == "__main__": import types import gv g = gv.digraph('exceptions') gv.setv(g, 'overlap', 'false') gv.setv(g, 'splines', 'true') gv.setv(g, 'pad', '1') for item in dir(__builtins__): attr = getattr(__builtins__, item) if type(attr) is types.TypeType and \ issubclass(attr, BaseException): tail = None head = None for i in attr.__mro__: nodename = i.__name__ tail = gv.findnode(g, nodename) if not tail: tail = gv.node(g, nodename) if head and not gv.findedge(tail, head): gv.edge(tail, head) head = tail gv.layout(g, 'neato') gv.render(g, 'png', 'built-in_exceptions.png')
視覺化結果如下圖所示:(大圖連結)
由圖上可以看到,例外物件的基底類別是BaseException,而Exception繼承BaseException,提供給所有內建的、系統的與使用者定義的例外去繼承Exception,這裡可以看到Python例外分成兩大類,一個是StandardError,另一個是Warning。
###
沒有留言:
張貼留言