ch17: update from book draft
This commit is contained in:
15
17-it-generator/tree/step5/tree.py
Normal file
15
17-it-generator/tree/step5/tree.py
Normal file
@@ -0,0 +1,15 @@
|
||||
def tree(cls):
|
||||
yield cls.__name__, 0
|
||||
yield from sub_tree(cls, 1)
|
||||
|
||||
|
||||
def sub_tree(cls, level):
|
||||
for sub_cls in cls.__subclasses__():
|
||||
yield sub_cls.__name__, level
|
||||
yield from sub_tree(sub_cls, level+1)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
for cls_name, level in tree(BaseException):
|
||||
indent = ' ' * 4 * level
|
||||
print(f'{indent}{cls_name}')
|
||||
Reference in New Issue
Block a user