473,394 Members | 1,701 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,394 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 1308
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.