Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if A is superclass of B in Python

class p1(object): pass class p2(p1): pass 

So p2 is the subclass of p1. Is there a way to find out programmatically that p1 is [one of] the superclass[es] of p2 ?

like image 396
Andz Avatar asked Dec 21 '09 07:12

Andz


People also ask

How do you check super class in Python?

Python issubclass() is built-in function used to check if a class is a subclass of another class or not. This function returns True if the given class is the subclass of given class else it returns False . Return Type: True if object is subclass of a class, or any element of the tuple, otherwise False.

What is Getattr () used for?

The getattr() function returns the value of the specified attribute from the specified object.

How do you identify a superclass?

To determine the superclass of a class, you invoke the getSuperclass method. This method returns a Class object representing the superclass, or returns null if the class has no superclass. To identify all ancestors of a class, call getSuperclass iteratively until it returns null.

What is __ Subclasshook __ in Python?

It returns True when a class is found to be subclass of a ABC class, it returns False if it is not and returns NotImplemented if the subclass check is continued with the usual mechanism. This method is defined in the ABC class with some conditions.


1 Answers

using <class>.__bases__ seems to be what you're looking for...

>>> class p1(object): pass >>> class p2(p1): pass >>> p2.__bases__ (<class '__main__.p1'>,) 
like image 72
user235859 Avatar answered Oct 14 '22 08:10

user235859