Get the name of the superlcass?
Remco Gerlich
scarblac-spamtrap at pino.selwerd.nl
Thu Jul 20 03:56:02 EDT 2000
Thomas Weholt wrote in comp.lang.python:
> I`m subclassing from a superclass. I can get the name of the subclass
> by using Subclass.__class__.name. But I would like to get the name of
> the superclass as well, but not lose the option of getting the
> subclass either.
>
> Ex. I got a class Structure, and a sublcass Building. I want to check
> in a list for structures, then see if any of them are buildings.
Use isinstance(). isinstance(object, class) is 1 if the object is an
instance of the class or one of its subclasses.
class Structure:
pass
class Building(Structure):
pass
generic_structue = Structure()
some_building = Building()
Now
isinstance(some_building, Structure) == 1
isinstance(some_building, Building) == 1
isinstance(generic_structure, Structure) == 1
isinstance(generic_structure, Building) == 0
If you want to know the superclasses of a class, use class.__bases__
(like Building.__bases__[0].__name__ == "Structure"). But using
isinstance is probably better.
--
Remco Gerlich, scarblac at pino.selwerd.nl
9:54am up 135 days, 22:03, 6 users, load average: 0.40, 0.19, 0.12
More information about the Python-list
mailing list