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 ?
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.
The getattr() function returns the value of the specified attribute from the specified object.
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.
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.
using <class>.__bases__ seems to be what you're looking for...
>>> class p1(object): pass >>> class p2(p1): pass >>> p2.__bases__ (<class '__main__.p1'>,)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With