HomeLinuxPython issubclass() Operate

Python issubclass() Operate


The high-level language Python is constructed on OOP protocols. In Python, all the pieces is taken into account to be an object. The “object” corresponds to a perform, a category, or a way. The inbuilt methodology named “issubclass()” is utilized in Python to confirm whether or not the required class is the derived class of one other class or not.

This tutorial will discover an in depth information on Python’s “issubclass()” perform by way of the under content material:

What’s the “issubclass()” Operate in Python?

In Python, the built-in “issubclass()” perform is utilized to confirm whether or not the required class is the subclass of one other specified class. If the actual class is a subclass of the given class, this perform retrieves “True”; in any other case, it retrieves “False”.

Syntax

issubclass(object, subclass)

 
Parameters

Within the above syntax, the “object” parameter specifies the category kind to be checked, and the “subclass” parameter signifies a category object or a tuple of sophistication objects.

Return Worth

The “issubclass()” perform retrieves “True” if the required class is the subclass of a category or any factor of the tuple and retrieves “False” in any other case.

Instance 1: Making use of the “issubclass()” Operate to Decide the Subclass

The under code is utilized to confirm whether or not the required class is the subclass of one other class or not:

class func1:
  age = 34
class func2(func1):
  title = “Joseph”
  age = func1
print(issubclass(func2, func1))

 
Within the above code:

    • The category named “func1” is outlined and the member variable named “age” is initialized.
    • The category named “func2” is said by accepting the “func1” class as an argument.
    • In its definition, the member variables “title” and “age” are initialized.
    • The “issubclass()” perform then takes the category “func2” as its first argument and the category “func1” as its second argument to test whether or not the required class (latter) is the subclass of the opposite class (former).

Output


Right here, it may be implied that the required class “func2” is the subclass of the opposite class “func1”.

Instance 2: Making use of the “issubclass()” Operate to Decide Whether or not the Constructed-in Class is a Subclass

The next code is utilized to confirm/test whether or not the built-in class is a subclass of one other 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, listing)
print(‘nString is the Subclass of Record: ‘, z)

 
Within the above code strains, the “issubclass()” perform is used a number of instances and takes the built-in lessons “bool”, “float” and “str”, respectively to test whether or not these lessons are subclasses of the opposite specified class i.e., “int” in every case or not.

Output


Based mostly on the above output, the “True” worth represents that the required class is the subclass of one other class, and the “False” represents that it isn’t.

Conclusion

The built-in Python “issubclass()” perform is utilized to confirm whether or not the actual class is the subclass of one other explicit class. It can retrieve “True” if the actual class is a subclass and “False” in any other case. This perform can even test if the built-in lessons are subclasses or not. This Python tutorial delivered a radical information on the “issubclass()” perform utilizing quite a few examples.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments