Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old January 16th, 2006, 12:55 PM
Charles Krug
Guest
 
Posts: n/a
Default 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.



  #2  
Old January 16th, 2006, 01:45 PM
Diez B. Roggisch
Guest
 
Posts: n/a
Default 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
  #3  
Old January 16th, 2006, 03:05 PM
Pierre Barbier de Reuille
Guest
 
Posts: n/a
Default 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]
  #4  
Old January 16th, 2006, 04:45 PM
Duncan Booth
Guest
 
Posts: n/a
Default 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()
  #5  
Old January 16th, 2006, 04:45 PM
Alex Martelli
Guest
 
Posts: n/a
Default 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
  #6  
Old January 19th, 2006, 12:05 AM
Charles Krug
Guest
 
Posts: n/a
Default 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.

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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.
Post your question now . . .
It's fast and it's free

Popular Articles