0% found this document useful (0 votes)
8 views

The_isinstance_and_issubclass_functions_1731993662

Uploaded by

Syed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

The_isinstance_and_issubclass_functions_1731993662

Uploaded by

Syed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

The isinstance() and issubclass()

functions
Birane Koundoul (University Alioune Diop of Bambey - Senegal)

[email protected]

November 18, 2024

PYTHON
Summary
xplanation of the isinstance() function ...............................................................................................2
Syntax .......................................................................................................................................... 2
Behavior ...................................................................................................................................... 2
Simple example ...........................................................................................................................3
Example with several types (tuple) ............................................................................................ 3
Common uses .............................................................................................................................. 3
Explanation of the issubclass() function .............................................................................................4
Syntax .........................................................................................................................................5
Behavior .................................................................................................................................... 5
A simple example .................................................................................................................... 5
Example with several classes (tuple) ............................................................................ 5
Multiple inheritance ............................................................................................................ 6
Common use ................................................................................................................................ 6

Explanation of the isinstance() function


The isinstance function in Python is used to check whether an object
is an instance of a class or type, or of one of the given classes or
types. It is commonly used in type checking to ensure that objects
passed to a function or manipulated in code are of the expected type.

Syntax

isinstance(obj, class_or_tuple)

 obj: The object you wish to test.

 class_or_tuple: The class, type, or tuple of classes/types against which

you wish to check the object.

Behavior

 If obj is an instance of class_or_tuple, the function returns True.

 Otherwise, it returns False.


Simple example

# Check if 5 is an integer
print(isinstance(5, int)) # True

# Check if "Hello" is a string


print(isinstance("Hello", str)) # True

Example with several types (tuple)

We can pass a tuple of types to isinstance, which checks whether the


object is an instance of one of the types mentioned in the tuple.
# Check if 3.14 is either an int or a float
print(isinstance(3.14, (int, float))) # True

Common uses

1. Type checking in functions: This can be useful when we want to


validate that arguments passed to a function are of the right
type.
2. Inheritance: isinstance returns True even if the object is an
instance of a subclass of the specified class.

class Animal:
pass

class Dog(Animal):
pass

my_dog = Dog()

# Checks if my_dog is an instance of Animal (or a subclass)


print(isinstance(my_dog, Animal)) # True

In short, isinstance is a flexible and essential method for dynamic


type control in scenarios where we need to know with certainty to
which type or class an object belongs.

Explanation of the issubclass() function


The issubclass() function in Python is used to check whether a class
is a subclass of another class. This is particularly useful in
contexts where you're working with class hierarchies and need to know
whether one class inherits from another.
Syntax

issubclass(class, class_or_tuple)

 class: The class we want to test.

 class_or_tuple: The class or tuple of classes against which we want to

check the subclass relationship.

Behavior

 If class is a subclass of class_or_tuple, the function returns True.

 Otherwise, it returns False.

A simple example

class Animal:
pass

class Dog(Animal):
pass

# Check if Dog is a subclass of Animal


print(issubclass(Dog, Animal)) # True

# Check if Animal is a subclass of Dog


print(issubclass(Animal, Dog)) # False

Example with several classes (tuple)

We can check whether a class is a subclass of several classes by


passing a tuple of classes to issubclass.

class Bird(Animal):
pass

# Check if Dog is a subclass of Animal or Bird


print(issubclass(Dog, (Animal, Bird))) # True

Multiple inheritance

In the case of multiple inheritance, issubclass will also work to


check whether a class inherits from several parent classes.

class Mammifere:
pass

Reptile class:
pass

class Dragon(Mammal, Reptile):


pass

# Check if Dragon is a subclass of Mammifere


print(issubclass(Dragon, Mammifere)) # True

# Check if Dragon is a subclass of Reptile


print(issubclass(Dragon, Reptile)) # True

Note

issubclass() also works with the object base class, as all Python
classes inherit from this class.

# Every class is a subclass of object


print(issubclass(Dog, object)) # True

Common use

 Inheritance check: To ensure that a class inherits from another


class, which is useful in plugin systems, frameworks or any
system where the parent-child relationship is important.
 Polymorphism: To validate that certain classes inherit from a
common base class, often as part of object-oriented design.
In short, issubclass() is a powerful tool for managing and verifying
relationships between classes in Python's inheritance hierarchies.

You might also like