
January 16th, 2006, 12:55 PM
| | | Trying to generate a list of the subclasses of C
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:
[color=blue][color=green][color=darkred]
>>> issubclass(D,C)[/color][/color][/color]
True
but
[color=blue][color=green][color=darkred]
>>> issubclass(classC.__dict__['D'], C)[/color][/color][/color]
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. | 
January 16th, 2006, 01:45 PM
| | | Re: Trying to generate a list of the subclasses of C
> The end result I'm after is an automatically generated dictionary[color=blue]
> 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.[/color]
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 | 
January 16th, 2006, 03:05 PM
| | | Re: Trying to generate a list of the subclasses of C
Charles Krug a écrit :[color=blue]
> 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
>[/color]
Where is C defined ?
[color=blue][color=green][color=darkred]
>>> import classC[/color][/color][/color]
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
[color=blue]
> Which gives me this:
>
> <class '__main__.C'>
> C
> CSubclasses
> D
> E
> __builtins__
> __doc__
> __file__
> __name__
>
> However when I do this from the command line:
>
>[color=green][color=darkred]
>>>>issubclass(D,C)[/color][/color]
>
> True
>
> but
>
>[color=green][color=darkred]
>>>>issubclass(classC.__dict__['D'], C)[/color][/color]
>
> 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.
>
>
>[/color] | 
January 16th, 2006, 04:45 PM
| | | Re: Trying to generate a list of the subclasses of C
Charles Krug wrote:
[color=blue]
> 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.[/color]
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() | 
January 16th, 2006, 04:45 PM
| | | Re: Trying to generate a list of the subclasses of C
Charles Krug <cdkrug@aol.com> wrote:
...[color=blue]
> I'm trying to create a list of all of C's subclasses:[/color]
There's a class method for that very purpose:
[color=blue][color=green][color=darkred]
>>> class C(object): pass[/color][/color][/color]
....[color=blue][color=green][color=darkred]
>>> class D(C): pass[/color][/color][/color]
....[color=blue][color=green][color=darkred]
>>> class E(C): pass[/color][/color][/color]
....[color=blue][color=green][color=darkred]
>>> C.__subclasses__()[/color][/color][/color]
[<class '__main__.D'>, <class '__main__.E'>][color=blue][color=green][color=darkred]
>>>[/color][/color][/color]
Alex | 
January 19th, 2006, 12:05 AM
| | | Re: Trying to generate a list of the subclasses of C
On 2006-01-16, Alex Martelli <aleax@mail.comcast.net> wrote:[color=blue]
> Charles Krug <cdkrug@aol.com> wrote:
> ...[color=green]
>> I'm trying to create a list of all of C's subclasses:[/color]
>
> There's a class method for that very purpose:
>[color=green][color=darkred]
>>>> class C(object): pass[/color][/color]
> ...[color=green][color=darkred]
>>>> class D(C): pass[/color][/color]
> ...[color=green][color=darkred]
>>>> class E(C): pass[/color][/color]
> ...[color=green][color=darkred]
>>>> C.__subclasses__()[/color][/color]
> [<class '__main__.D'>, <class '__main__.E'>][color=green][color=darkred]
>>>>[/color][/color]
>
>
> Alex[/color]
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. |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|