Python issubclass() Built in Function

The issubclass() function checks if a class is a subclass of another class. It returns True if the first argument is a subclass of the second argument, otherwise False.

Here are some examples of using the issubclass() function:

# Example 1: Check for built-in types
print(issubclass(int, object)) # True
print(issubclass(str, object)) # True
print(issubclass(bool, int)) # True
print(issubclass(list, tuple)) # False


# Example 2: Check for custom classes using inheritance
class Animal:
    pass


class Dog(Animal):
    pass


class Cat(Animal):
    pass


class Bird:
    pass


dog = Dog()
cat = Cat()
bird = Bird()


print(issubclass(Dog ,Animal)) # True
print(issubclass(Cat ,Animal)) # True
print(issubclass(Bird ,Animal)) # False
print(issubclass(dog ,Dog)) # TypeError: issubclass() arg 1 must be a class

If you have any questions about this code, you can drop a line in comment.

Comments

Popular posts from this blog

Python chr() Built in Function

Python The _ Operator

Python bool() Built in Function

Python Printing Readable Results

Python One Liner Functions