| by Arround The Web | No comments

Python issubclass() Function

The high-level language Python is built on OOP protocols. In Python, everything is considered to be an object. The “object” corresponds to a function, a class, or a method. The inbuilt method named “issubclass()” is utilized in Python to verify whether the specified class is the derived class of another class or not.

This tutorial will explore a detailed guide on Python’s “issubclass()” function via the below content:

What is the “issubclass()” Function in Python?

In Python, the built-in “issubclass()” function is utilized to verify whether the specified class is the subclass of another specified class. If the particular class is a subclass of the given class, this function retrieves “True”; otherwise, it retrieves “False”.

Syntax

issubclass(object, subclass)

 
Parameters

In the above syntax, the “object” parameter specifies the class type to be checked, and the “subclass” parameter indicates a class object or a tuple of class objects.

Return Value

The “issubclass()” function retrieves “True” if the specified class is the subclass of a class or any element of the tuple and retrieves “False” otherwise.

Example 1: Applying the “issubclass()” Function to Determine the Subclass

The below code is utilized to verify whether the specified class is the subclass of another class or not:

class func1:
  age = 34
class func2(func1):
  name = "Joseph"
  age = func1
print(issubclass(func2, func1))

 
In the above code:

    • The class named “func1” is defined and the member variable named “age” is initialized.
    • The class named “func2” is declared by accepting the “func1” class as an argument.
    • In its definition, the member variables “name” and “age” are initialized.
    • The “issubclass()” function then takes the class “func2” as its first argument and the class “func1” as its second argument to check whether the specified class (latter) is the subclass of the other class (former).

Output


Here, it can be implied that the specified class “func2” is the subclass of the other class “func1”.

Example 2: Applying the “issubclass()” Function to Determine Whether the Built-in Class is a Subclass

The following code is utilized to verify/check whether the built-in class is a subclass of another specified class:

x = issubclass(bool, int)
print('Bool is the Subclass of Integer: ', x)
y = issubclass(float, int)
print('\nFloat is the Subclass of Integer: ', y)
z = issubclass(str, list)
print('\nString is the Subclass of List: ', z)

 
In the above code lines, the “issubclass()” function is used multiple times and takes the built-in classes “bool”, “float” and “str”, respectively to check whether these classes are subclasses of the other specified class i.e., “int” in each case or not.

Output


Based on the above output, the “True” value represents that the specified class is the subclass of another class, and the “False” represents that it is not.

Conclusion

The built-in Python “issubclass()” function is utilized to verify whether the particular class is the subclass of another particular class. It will retrieve “True” if the particular class is a subclass and “False” otherwise. This function can also check if the built-in classes are subclasses or not. This Python tutorial delivered a thorough guide on the “issubclass()” function using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply