Trying to generate a list of the subclasses of C

C

Charles Krug

List:

I have this:

# classC.py

class C(object): pass

class D(C): pass

class E(C): pass

def CSubclasses():
for name in dir(): pass

I'm trying to create a list of all of C's subclasses:

import classC

print C
aList = []
for name in dir(classC):
print name,
try:
if issubclass(classC.__dict__[name], C):
print classC.__dict__[name]
else:
print
except TypeError:
print

Which gives me this:

<class '__main__.C'>
C
CSubclasses
D
E
__builtins__
__doc__
__file__
__name__

However when I do this from the command line:
True

but
issubclass(classC.__dict__['D'], C)
False

So my approach is flawed.

The end result I'm after is an automatically generated dictionary
containing instaces of the subclasses keyed by the subclass names:

{'D':D(), 'E':E(), . . . }

I can see the information I need in the module's __dict__ and by using
the dir() method, but I'm not having much success extracting it.
 
D

Diez B. Roggisch

The end result I'm after is an automatically generated dictionary
containing instaces of the subclasses keyed by the subclass names:

{'D':D(), 'E':E(), . . . }

I can see the information I need in the module's __dict__ and by using
the dir() method, but I'm not having much success extracting it.

Use a metaclass:


subclassesofc = []

class SubclassCollector(type):

def __new__(cls, name, bases, dict):
subclassesofc.append(name)
return type.__new__(cls, name, bases, dict)

class C(object):
__metaclass__ = SubclassCollector



class A(C):
pass

print subclassesofc



Regards,

Diez
 
P

Pierre Barbier de Reuille

Charles Krug a écrit :
List:

I have this:

# classC.py

class C(object): pass

class D(C): pass

class E(C): pass

def CSubclasses():
for name in dir(): pass

I'm trying to create a list of all of C's subclasses:

import classC

print C
aList = []
for name in dir(classC):
print name,
try:
if issubclass(classC.__dict__[name], C):
print classC.__dict__[name]
else:
print
except TypeError:
print

Where is C defined ?

does not define the name C in the current scope ... thus, you should write :

for name in dir(classC):
print name,
try:
if issubclass(classC.__dict__[name], classC.C):
print classC.__dict__[name]
else:
print
except TypeError:
print

and it gives :

C <class 'test_subclass.C'>
CSubclasses
D <class 'test_subclass.D'>
E <class 'test_subclass.E'>
__builtins__
__doc__
__file__
__name__

.... which is exactly what you want !

Pierre
Which gives me this:

<class '__main__.C'>
C
CSubclasses
D
E
__builtins__
__doc__
__file__
__name__

However when I do this from the command line:

issubclass(D,C)
True

but

issubclass(classC.__dict__['D'], C)

False

So my approach is flawed.

The end result I'm after is an automatically generated dictionary
containing instaces of the subclasses keyed by the subclass names:

{'D':D(), 'E':E(), . . . }

I can see the information I need in the module's __dict__ and by using
the dir() method, but I'm not having much success extracting it.
 
D

Duncan Booth

Charles said:
The end result I'm after is an automatically generated dictionary
containing instaces of the subclasses keyed by the subclass names:

{'D':D(), 'E':E(), . . . }

I can see the information I need in the module's __dict__ and by using
the dir() method, but I'm not having much success extracting it.

Try this:

class C(object): pass

class D(C): pass

class E(C): pass

def CSubclasses():
return dict((cls.__name__, cls) for cls in C.__subclasses__())

print CSubclasses()
 
C

Charles Krug

There's a class method for that very purpose:



Alex

Exactly what I was looking for, thanks.

It stuck in my brain that there was a way to do this, but I couldn't lay
my mouse on it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top