473,287 Members | 1,564 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,287 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 1305
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")) ; ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.