Python Tutorial

Showing posts with label reflection. Show all posts
Showing posts with label reflection. Show all posts

Tuesday, September 4, 2012

python subclass list(reflection)



Sometimes we need get list of subclass of a class. It is very simple in python.


 
class MyClass(object): pass

class SubClassA(MyClass): pass
class SubClassB(MyClass): pass
class SubClassC(MyClass): pass

print([cls.__name__ for cls in vars()['MyClass'].__subclasses__()])




Output:
['SubClassA', 'SubClassB', 'SubClassC']