How can I handle the class hierarchy given by an object instance in Python?

Do I need to find the base class of a class in Python at all?

Given the following class definitions:

class A:
   def speak(self):
      print "Hi"

class B(A):
   def getName(self):
      return "Bob"

      

If I get an instance of an object, I can easily execute it by doing the following:

instance = B()
print B.__class__.__name__

      

Which prints the class name "B" as expected.

Is there anyway that the object instance is found to inherit from both the base class and the actual class?

Or is this just not how objects work in Python?

+2


source to share


4 answers


b = B()
b.__class__
b.__class__.__base__
b.__class__.__bases__
b.__class__.__base__.__subclasses__()

      



I highly recommend checking out ipython and using the tab :-)

+5


source


The validation module is also very efficient:



>>> import inspect

>>> inst = B()
>>> inspect.getmro(inst.__class__)
(<class __main__.B at 0x012B42A0>, <class __main__.A at 0x012B4210>)

      

+6


source


Another way to get the class hierarchy is to access the mro attribute :

class A(object):
    pass
class B(A):
    pass
instance = B()
print(instance.__class__.__mro__)
# (<class '__main__.B'>, <class '__main__.A'>, <type 'object'>)

      

Note that in Python 2.x you must use "new style" objects to make sure they have mro . You do this by declaring

class A(object):

      

instead

class A():

      

See http://www.python.org/doc/newstyle/ and http://www.python.org/download/releases/2.3/mro/ for more details on new style objects and mro (method resolution order) ...

In Python 3.x, all objects are objects of a new type, so you can use mro and just declare objects like this:

class A():

      

+3


source


If instead of detecting the base layer (i.e. reflection) you know the desired class in advance, you can use the following built-in functions

eg:

# With classes from original Question defined
>>> instance = A()
>>> B_instance = B()
>>> isinstance(instance, A)
True
>>> isinstance(instance, B)
False
>>> isinstance(B_instance, A) # Note it returns true if instance is a subclass
True
>>> isinstance(B_instance, B)
True
>>> issubclass(B, A)
True

      

isinstance (object, classinfo)     Returns true if the object argument is an instance of the classinfo argument or (direct or indirect) subclass. Also return true if classinfo is an object and the type object is an object of that type. If the object is not an instance of a class or an object of this type, the function always returns false. If classinfo is not a class object or object of type, it can be a tuple of a class or type of objects, or it can recursively contain other such tuples (no other sequence of types is accepted). If classinfoinfo is not a class, type, or tuple of classes, types, and such tuples, TypeError is fixed. Changed in version 2.2: Support for tuple type information has been added.

issubclass (class, class)     Returns true if the class is a subclass (direct or indirect) of the info class. the class is considered a subclass itself. classinfo can be a tuple of class objects, in which case every entry in classinfo will be checked. In any other case, a TypeError was raised. Changed in version 2.3: Support for information type tuples has been added.

0


source







All Articles