How to find out which functions exist?

M

mrstephengross

Let's say I have a python file with a base class, and a few derived
classes:

class Base:
pass

class Derived1(Base):
pass

class Derived2(Base):
pass

Is there a way I can find out the classes that have been derived from
Base?

Thanks,
--Steve
 
M

Marc 'BlackJack' Rintsch

Let's say I have a python file with a base class, and a few derived
classes:

class Base:
pass

class Derived1(Base):
pass

class Derived2(Base):
pass

Is there a way I can find out the classes that have been derived from
Base?

Take a look at the `issubclass()` function.

Ciao,
Marc 'BlackJack' Rintsch
 
M

mrstephengross

Take a look at the `issubclass()` function.

Ok, I see how to use issubclass(). How can I get a list of classes
present in the file?

--Steve
 
M

Marc 'BlackJack' Rintsch

Ok, I see how to use issubclass(). How can I get a list of classes
present in the file?

import module
from inspect import getmembers, isclass

classes = getmembers(module, isclass)


Ciao,
Marc 'BlackJack' Rintsch
 
G

Gabriel Genellina

En Tue, 23 Oct 2007 18:10:04 -0300, mrstephengross
Let's say I have a python file with a base class, and a few derived
classes:

class Base:
pass

class Derived1(Base):
pass

class Derived2(Base):
pass

Is there a way I can find out the classes that have been derived from
Base?

If Base were a new-style class, you could use Base.__subclasses__()
 
M

mrstephengross

import module
from inspect import getmembers, isclass
classes = getmembers(module, isclass)

Ok, this makes sense. How can I do it inside the .py file I'm working
on? That is, consider this:

class A:
pass
class B:
pass
import inspect
print inspect.getmembers(<this file>, inspect.isclass) # how can I
express <this file> ?

Thanks again,
--Steve
 
D

Diez B. Roggisch

mrstephengross said:
Ok, this makes sense. How can I do it inside the .py file I'm working
on? That is, consider this:

class A:
pass
class B:
pass
import inspect
print inspect.getmembers(<this file>, inspect.isclass) # how can I
express <this file> ?

Then you can use globals(), like this:

classes = [v for v in globals().values() if isclass(v)]

Diez
 
M

Marc 'BlackJack' Rintsch

Ok, this makes sense. How can I do it inside the .py file I'm working
on? That is, consider this:

class A:
pass
class B:
pass
import inspect
print inspect.getmembers(<this file>, inspect.isclass) # how can I
express <this file> ?

If you want the objects from the current module you can simply look at the
`globals()` dictionary.

from inspect import isclass
from itertools import ifilter

classes = set(ifilter(isclass, globals().itervalues()))

Ciao,
Marc 'BlackJack' Rintsch
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top