Remember
issubclass() checks a class’s inheritance relationship. A class is also considered a subclass of itself.
issubclass() checks a class’s inheritance relationship. A class is also considered a subclass of itself.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
class Parent:
pass
class Child(Parent):
value = 3
print(issubclass(Child, Parent))
print(issubclass(Child, Child))Top-level scope
class ParentObject 3 · typeclass Childissubclass() checks a class’s inheritance relationship. A class is also considered a subclass of itself.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
class Parent:
pass
class Child(Parent):
value = 3
print(issubclass(Child, Parent))
print(issubclass(Child, Child))
|
v
DISPLAYED RESULT
True
True
FINAL BINDINGS
Parent --> object 1 (type)
Child --> object 3 (type)
object 1: class Parent
object 3: class ChildChoose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
class Parent:
pass
class Child(Parent):
value = 3
print(issubclass(Child, Parent))
print(issubclass(Child, Child))Top-level scope
class ParentObject 3 · typeclass ChildThe example has finished. Compare the final output with the values and references above.
True True
What do you expect this program to print?
class Parent:
pass
class Child(Parent):
value = 3
print(issubclass(Child, Parent))
print(issubclass(Child, Child))issubclass() FunctionThe issubclass() function checks if a class is a subclass of another class or a tuple of classes. It is used in object-oriented programming to verify inheritance relationships.
issubclass(class, classinfo)
class – The class you want to check.classinfo – A class or a tuple of classes to check against.True if class is a subclass (direct or indirect) of classinfo.False otherwise.class Animal:
pass
class Dog(Animal):
pass
print(issubclass(Dog, Animal))
True
class Animal:
pass
class Mammal(Animal):
pass
class Cat(Mammal):
pass
print(issubclass(Cat, Animal))
True
Why? Because Cat → Mammal → Animal. So, Cat is an indirect subclass of Animal.
class Vehicle: pass
class Car(Vehicle): pass
print(issubclass(Car, (Vehicle, Animal)))
True
Returns True if Car is a subclass of any class in the tuple.
print(issubclass(5, int))
TypeError: issubclass() arg 1 must be a class
Note: You must pass a class as the first argument, not an instance.
issubclass(dog, Animal) ❌isinstance(dog, Animal) to check instancesissubclass() is often tested with isinstance(). Know the difference: one checks classes, the other checks instances.
issubclass(A, B) returns True if A is a subclass of BTypeError if the first argument is not a classCreate a class hierarchy with Shape, Polygon, and Triangle. Use issubclass() to check if Triangle is a subclass of Shape.
class Shape: pass
class Polygon(Shape): pass
class Triangle(Polygon): pass
print(issubclass(Triangle, Shape))
True
Connect what you noticed in the example with the idea below.
issubclass() checks a class’s inheritance relationship. A class is also considered a subclass of itself.
issubclass() accepts an ordinary instance as its first argument.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.