Getting a list of all classes derived from a base class
Dylan Moreland
dylan.moreland at gmail.com
Mon Apr 3 01:07:36 EDT 2006
Vijairaj R wrote:
> Hi,
> I have a requirement to findout all the classes that are derived from a
> single base class.
>
> This is how I do it currently.
>
> class Test:
> case = []
>
> class Test1(Test):
> Test.case.append("Test1")
>
> class Test2(Test):
> Test.case.append("Test2")
>
> 1. Is there a better way of doing this.
> 2. Is there a way to generalize the Test.case.append("TestN")
> statements to something like
> Test.case.append(__myclass__)
>
> --
> Warm Regards,
> Vijairaj
If you're willing to use metaclass madness:
class TestMeta(type):
def __init__(cls, name, bases, dct):
if bases == (object,): return # Prevent the appending of Test
cls.case.append(cls)
class Test(object):
__metaclass__ = TestMeta
case = []
class Test1(Test): pass
class Test2(Test): pass
print Test.case
More information about the Python-list
mailing list