পাইথনের বিল্ট-ইন এক্সসেপশন হায়ারার্কি দেখতে inspect মডিউলের getclasstree() ফাংশন ব্যবহার করা হয়।
এই কোডটি এক্সসেপশন ক্লাসের লিস্টকে নেস্টেড লিস্ট হায়ারার্কিতে সাজিয়ে প্রিন্ট করে। __subclasses__() ব্যবহার করে ইনহেরিটেন্স ট্রি অনুসরণ করে আউটপুট দেওয়া হয়।
উদাহরণ
import inspect
print("The class hierarchy for built-in exceptions is:")
inspect.getclasstree(inspect.getmro(BaseException))
def classtree(cls, indent=0):
print('.' * indent, cls.__name__)
for subcls in cls.__subclasses__():
classtree(subcls, indent + 3)
classtree(BaseException)
আউটপুট
The class hierarchy for built-in exceptions is: BaseException ... Exception ...... StandardError ......... TypeError ......... ImportError .......... ZipImportError ......... EnvironmentError .......... IOError .......... OSError ............. WindowsError ......... EOFError ......... RuntimeError .......... NotImplementedError ......... NameError .......... UnboundLocalError ......... AttributeError ......... SyntaxError .......... IndentationError ............. TabError ......... LookupError .......... IndexError .......... KeyError .......... CodecRegistryError ......... ValueError .......... UnicodeError ............. UnicodeEncodeError ............. UnicodeDecodeError ............. UnicodeTranslateError ......... AssertionError ......... ArithmeticError .......... FloatingPointError .......... OverflowError .......... ZeroDivisionError ......... SystemError .......... CodecRegistryError ......... ReferenceError ......... MemoryError ......... BufferError ...... StopIteration ...... Warning ......... UserWarning ......... DeprecationWarning ......... PendingDeprecationWarning ......... SyntaxWarning ......... RuntimeWarning ......... FutureWarning ......... ImportWarning ......... UnicodeWarning ......... BytesWarning ...... _OptionError ...... error ...... Error ...... TokenError ...... StopTokenizing ...... error ...... EndOfBlock ... GeneratorExit ... SystemExit ... KeyboardInterrupt