471,350 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

How to find out which functions exist?

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

Oct 23 '07 #1
7 1248
On Tue, 23 Oct 2007 21:10:04 +0000, mrstephengross wrote:
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
Oct 23 '07 #2
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

Oct 23 '07 #3
On Tue, 23 Oct 2007 21:51:20 +0000, mrstephengross wrote:
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
Oct 23 '07 #4
En Tue, 23 Oct 2007 18:10:04 -0300, mrstephengross
<mr************@hotmail.comescribió:
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__()

--
Gabriel Genellina

Oct 24 '07 #5
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

Oct 24 '07 #6
mrstephengross wrote:
>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?
Then you can use globals(), like this:

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

Diez
Oct 24 '07 #7
On Wed, 24 Oct 2007 15:09:02 +0000, mrstephengross wrote:
>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?
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
Oct 24 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Kay Schluehr | last post: by
13 posts views Thread by Noah Spitzer-Williams | last post: by
16 posts views Thread by Justin Hoffman | last post: by
29 posts views Thread by chellappa | last post: by
106 posts views Thread by cfmortgagepro | last post: by
15 posts views Thread by David T. Ashley | last post: by
7 posts views Thread by eduzea | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.