Function “Isinstance,” Class Object, and Class Book

Function isinstance reports whether an object is an instance of a class—that is, whether an object has a particular type:

 >>>​​ ​​isinstance(​​'abc'​​,​​ ​​str)
 True
 >>>​​ ​​isinstance(55.2,​​ ​​str)
 False

’abc’ is an instance of str, but 55.2 is not.

Python has a class called object. Every other class is based on it:

 >>>​​ ​​help(object)
 Help on class object in module builtins:
 
 class object
  | The most base type

Function isinstance reports that both ’abc’ and 55.2 are instances of class object:

 >>>​​ ​​isinstance(55.2,​​ ​​object)
 True
 >>>​​ ​​isinstance(​​'abc'​​,​​ ​​object)
 True

Even classes and functions are instances of object:

 >>>​​ ​​isinstance(str, ...

Get Practical Programming, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.