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 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
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
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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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:...
|
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?
|
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
|
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...
|
by: anand |
last post by:
***********************************************************************************************************
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
double...
|
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...
|
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...
|
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")) ;
...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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
|
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...
|
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,...
|
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...
| |