Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python issubclass() Function



ThePython issubclass() function is abuilt-in function used for verifying whether a given class is a subclass of the specified class. In object-oriented programming, a subclass is a class that extends the functionality of another class, referred to as its superclass or parent class.

If the given class is a subclass of the specified class, theissubclass() function returns True, otherwise, False. Note that a class is also a subclass of itself.

Syntax

The syntax of the Pythonissubclass() function is as follows −

issubclass(object, subclass)

Parameters

Following are the parameters of the Pythonissubclass() function −

  • object − This parameter specifies an object whose attribute needs to be searched.

  • subclass − This parameter represents a class object, or atuple of class object.

Return Value

The Pythonissubclass() function returns aBoolean value (either True or False).

issubclass() Function Examples

Practice the following examples to understand the use ofissubclass() function in Python:

Example: Use of issubclass() Function

The following is an example of the Python issubclass() function. In this, we have created a class along with its subclass and, trying to verify whether the class named "ClassChild" is subclass or not.

class ClassParent:   passclass ClassChild(ClassParent):   passoutput = issubclass(ClassChild, ClassParent)print("Is ClassChild is child class of ClassParent:", output)

On executing the above program, the following output is generated −

Is ClassChild is child class of ClassParent: True

Example: issubclass() Function With Multiple Inheritance

The issubclass() function also works with multipleinheritance. Here, we have defined two parent classes along with a child class. Then, tried to check if the class "ClassTata" is a child class of both defined classes.

class ClassVehicle:   passclass ClassCar:   passclass ClassTata(ClassVehicle, ClassCar):   passcheckOne = issubclass(ClassTata, ClassVehicle)checkTwo = issubclass(ClassTata, ClassCar)print("Is ClassTata is child class of ClassVehicle:", checkOne) print("Is ClassTata is child class of ClassCar:", checkTwo)

The following is the output obtained by executing the above program −

Is ClassTata is child class of ClassVehicle: TrueIs ClassTata is child class of ClassCar: True

Example: Checking Subclass Using issubclass() Function

In Python, all classes including "int" are subclasses of the "object" class. To verify this fact, we have used the issubclass() function in the below example code.

res = issubclass(int, object)print("Is int is subclass of object class:", res)

The following output is obtained by executing the above program -

Is int is subclass of object class: True

Example: Use of issubclass() Function With Tuples

The issubclass() function also accepts arguments in the form of a tuple. In this example, we are passing a tuple of classes to check whether the class named "ClassTata" is a subclass of specified objects or not.

class ClassVehicle:   passclass ClassCar(ClassVehicle):   passclass ClassTata(ClassCar):   passchecker = issubclass(ClassTata, (ClassCar, ClassVehicle))print("Is ClassTata is child class:", checker)

The above program, on executing, displays the following output −

Is ClassTata is child class: True
python_built_in_functions.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp