The 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 base class of the class hierarchy. |
The function isinstance reports that both ’abc’ and 55.2 are instances of the class object:
| | >>> isinstance(55.2, object) |
| | True |
| | >>> isinstance('abc', object) |
| | True |
Even classes and functions are instances of object:
| | >>> isinstance(str, object) |
| | True |
| | >>> isinstance(max, object) |
| | True |
What’s happening here is that every class in Python is derived from class object, and so every instance of every class is an object.
Using object-oriented terminology, we say that class object is the superclass of class str, and class str is a subclass of class object. The superclass information is available in the help documentation for a type. For example, the following shows that the superclass of class int is object:
| | >>> help(int) |
| | Help on class int in module builtins: |
| | |
| | class int(object) |
Here, you see that the class SyntaxError is a subclass of the class Exception (you will read more about them in Chapter 17, ):
| | >>> help(SyntaxError) |
| | Help on class SyntaxError in module builtins: |
| | |
| | class SyntaxError(Exception) |
Class object has the following attributes (attributes are variables inside a class that refer to methods, functions, variables, or even other classes):
| | >>> dir(object) |
| | ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', |
| | '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', |
| | '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', |
| | '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', |
| | '__subclasshook__'] |
Every class in Python, including ones that you define, automatically inherits these attributes from class object. More generally, every subclass inherits the features of its superclass. Inheritance is a powerful tool; it helps avoid much duplicate code and makes interactions between related types consistent.
Let’s try this out. Here is the simplest class that you can write:
| | >>> class Book: |
| | ... """Information about a book.""" |
| | ... |
Just as keyword def tells Python that you’re defining a new function, keyword class signals that you’re defining a new type.
Much like str is a type, Book is a type:
| | >>> type(str) |
| | <class 'type'> |
| | >>> type(Book) |
| | <class 'type'> |
Your Book class isn’t empty, either, because it has inherited all the attributes of class object:
| | >>> dir(Book) |
| | ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', |
| | '__firstlineno__', '__format__', '__ge__', '__getattribute__', '__getstate__', |
| | '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', |
| | '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', |
| | '__setattr__', '__sizeof__', '__static_attributes__', '__str__', |
| | '__subclasshook__', '__weakref__'] |
If you look carefully, you’ll see that this list is nearly identical to the output for dir(object). There are three additional attributes in the class Book; every subclass of the class object automatically has these attributes in addition to the inherited ones:
| | >>> set(dir(Book)) - set(dir(object)) |
| | {'__weakref__', '__module__', '__firstlineno__', '__dict__', |
| | '__static_attributes__'} |
You’ll get to those attributes later on in this chapter. First, let’s create a Book object and give that Book a title and a list of authors:
| | >>> ruby_book = Book() |
| | >>> ruby_book.title = 'Programming Ruby' |
| | >>> ruby_book.authors = ['Thomas', 'Fowler', 'Hunt'] |
The first assignment statement creates a Book object and then assigns that object to the variable ruby_book. The second assignment statement creates a title variable inside the Book object; that variable refers to the string ’Programming Ruby’. The third assignment statement creates a variable authors, also inside the Book object, which refers to the list of strings [’Thomas’, ’Fowler’, ’Hunt’].
The variables title and authors are called instance variables because they are variables inside an instance of a class. You can access these instance variables through the variable ruby_book:
| | >>> ruby_book.title |
| | 'Programming Ruby' |
| | >>> ruby_book.authors |
| | ['Thomas', 'Fowler', 'Hunt'] |
In the expression ruby_book.title, Python finds variable ruby_book, then sees the dot and goes to the memory location of the Book object, and then looks for variable title. Here is a model of computer memory for this situation:

You can even get help on your Book class:
| | >>> help(Book) |
| | Help on class Book in module __main__: |
| | |
| | class Book(builtins.object) |
| | | Information about a book. |
| | | |
| | | Data descriptors defined here: |
| | | |
| | | __dict__ |
| | | dictionary for instance variables |
| | | |
| | | __weakref__ |
| | | list of weak references to the object |
The first line tells you that you asked for help on the class Book. After that is the header for class Book; the (builtins.object) part tells you that Book is a subclass of class object. The next line shows the Book docstring. The last section is titled “Data descriptors.” The descriptors are special pieces of information that Python stores with every user-defined class.