473,385 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1307
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Kay Schluehr | last post by:
Hi all, thanks for Your attention ! I think my proposal was more in mind of Rons modified exec than Pythons lambda. When George proposed his unpacking behavoir for list-comps as a pack of...
13
by: Noah Spitzer-Williams | last post by:
Hello guys, I would like to do something seemingly simple: find out if an element in an array that is passed to my function exists. I used to think I could just do: if (arr) ... However, if...
16
by: Justin Hoffman | last post by:
This is a question concerning query optimisation. Sorry if it's a bit long, but thanks to anyone who has the patience to help - This is my first post here... If I have two tables:...
4
by: BUX | last post by:
They exist in Visual Studio .Net , but I do not know the name of dll to create the reference! Can you please help me?
29
by: chellappa | last post by:
hi all I need to write a program to find mac address of a my computer using libaries, is this possible? How? thanks
106
by: cfmortgagepro | last post by:
Hi, I know that I'm an extreme newb by asking this overly beaten question, but I am leaning toward C#, becuase the perception is that it is better to learn than VB.Net. I guess it makes you...
18
by: anand | last post by:
*********************************************************************************************************** #include<stdio.h> #include<conio.h> #include<math.h> void main() { double...
1
by: TheScullster | last post by:
Hi all Bit of an obscure one this, but hopefully someone will be able to help. 4 years ago we had a potentially catastrophic problem with our Access database. From memory it was written in...
15
by: David T. Ashley | last post by:
I'll be using gcc under Red Hat Enterprise Linux. Where do I find information about the low-level calls to the operating system from compiled 'C'? For example, if I want to cause a process to...
7
by: eduzea | last post by:
I have some code that compiles with gcc but fails with MS VC 8. I would like to know which one is following the C++ standard. istream& istr = false ? cin : ( * new ifstream("test.txt")) ; ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.