Python isinstance() Function

Python3 内置函数Python3 Built-in Functions


isinstance()It is a built-in function in Python used to check whether an object is an instance of the specified type (or any type in a tuple of types).

isinstance()It is the most commonly used type-checking function in Python. It not only checks the exact type but also considers inheritance relationships (subclasses are also instances of their parent class).

Term Definition: isinstance 是 is an instance(是一个实例)的缩写。


Basic Syntax and Parameters

Syntax

isinstance(object, classinfo)

Parameter Description

  • Parameter object:
    • Type: Any object
    • Description: The object to check.
  • Parameter classinfo:
    • Type: A type, a tuple of types, or an abstract base class
    • Description: The type to check against. Can be a single type or a tuple of types.

Function Description

  • Return value: Returns a boolean valueTrueorFalse。
  • Feature: Returns True if object is an instance of a subclass of classinfo.

Examples

Example 1: Basic Usage

Example

# Check basic types
print(isinstance(123, int))           # Output: True
print(isinstance(3.14, float))        # Output: True
print(isinstance("hello", str))       # Output: True
print(isinstance([1, 2], list))       # Output: True
print(isinstance({"a": 1}, dict))     # Output: True

# Check None
print(isinstance(None, type(None)))   # Output: True

# Check boolean values (note: bool is a subclass of int)
print(isinstance(True, bool))         # Output: True
print(isinstance(True, int))          # Output: True (because bool inherits from int)

Expected output:

True
True
True
True
True
True
True
True

Code analysis:

  1. It can check various basic types.
  2. bool is a subclass of int, so True/False are also instances of int.

Example 2: Using a Tuple of Types

Example

# Check multiple types (using a tuple)
value = 42

print(isinstance(value, (int, float)))    # Output: True
print(isinstance(value, (str, list)))     # Output: False

# Check a mix of multiple types
value = "hello"
print(isinstance(value, (int, str, bytes))) # Output: True

# Used in conditional statements
def process_data(data):
    if isinstance(data, (list, tuple)):
        return sum(data)
    elif isinstance(data, (int, float)):
        return data * 2
    else:
        return str(data)

print(process_data([1, 2, 3]))     # Output: 6
print(process_data((1, 2, 3)))    # Output: 6
print(process_data(10))           # Output: 20
print(process_data(3.14))         # Output: 6.28
print(process_data("hello"))      # Output: hello

Expected output:

True
False
True
6
6
20
6.28
hello

Code analysis:

  • The second parameter can be a tuple of types, allowing multiple types to be checked at once.
  • This is very convenient when working with multiple data types.

Example 3: With Class Inheritance

Example

# Class inheritance example
class Animal:
    pass

class Dog(Animal):
    pass

class Cat(Animal):
    pass

# Create an instance
dog = Dog()
cat = Cat()
animal = Animal()

# isinstance takes inheritance into account
print(isinstance(dog, Dog))      # Output: True
print(isinstance(dog, Animal))   # Output: True (a subclass instance is also an instance of the parent class)
print(isinstance(dog, Cat))      # Output: False

print(isinstance(cat, Animal))   # Output: True
print(isinstance(animal, Dog))   # Output: False

Expected output:

True
True
False
True
False

isinstance()It checks the inheritance chain, so subclass instances are also instances of the parent class.


Python3 内置函数Python3 Built-in Functions


Other Extensions