liveBook · Manning

Isinstance

As explained in The Quick Python Book, Fourth Edition

isinstance is a Python built-in function that tests whether an object is an instance of a specified class or tuple of classes.

`isinstance(object, class)` checks whether an object is an instance of a class. The check includes inheritance: an instance of a subclass also passes a check against its base class. Use it when a function or method needs to validate an expected type. section 17.3

A subclass passes its base-class check

class Base: pass class Child(Base): pass isinstance(Child(), Child) # True isinstance(Child(), Base) # True isinstance(Base(), Child) # False section 17.3

Type checks can guard inputs and container contents:

  • A string-only dictionary can check keys and values with `isinstance(value, str)` and raise `TypeError` for a mismatch. section 17.11.2
  • A typed-list class can obtain an expected class with `type(example_element)`, then check initial elements with `isinstance`. A later version uses `type(element) != self.type` for an exact-type check instead. section 17.8
""" Create a dictionary that allows only strings for keys and values"""
 
class StringDict(dict):
    def __setitem__(self, key, value):
        if not isinstance(key, str):
            raise TypeError('key is not a string')     #1
        if not isinstance(value, str):
            raise TypeError('value is not a string')
        super().__setitem__(key, value)
 
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        key_error = any(not isinstance(_, str) 
                         for _ in self.keys())         #2
        if key_error:
            raise TypeError('key is not a string')
        value_error = any(not isinstance(_, str) 
                         for _ in self.values())       #2
        if value_error:
            raise TypeError('value is not a string')

The string-only dictionary checks assignments in `__setitem__`. Its initializer also checks the populated dictionary, because initial data may arrive as key-value pairs or another dictionary. `any` flags a non-string key or value. section 17.11.2

For instances, prefer `isinstance` when subclasses should count. `issubclass` instead checks relationships between classes. Before adding an explicit type check, consider whether the operation itself can handle an unsuitable object. section 17.3

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Explains inherited-instance behavior and works through container validation, without examining the built-in’s implementation.
Examples
Demonstrates subclass checks, a string-only dictionary, and a typed list whose allowed element type comes from an example value.
What it assumes
Assumes familiarity with Python classes and method calls while illustrating how inheritance affects instance checks.
What you can do after
Apply it via Python’s built-in `isinstance` to reject unsuitable inputs or container elements with `TypeError`.
How it connects
Connects instance checks to inheritance, `type`, `issubclass`, `TypeError`, and container special methods.

Also explained in

Compare how 2 books explain isinstance →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.