The implicit superclass – object

Each Python class definition has an implicit superclass: object. It's a very simple class definition that does almost nothing.

We can create instances of object, but we can't do much with them, because many of the special methods simply raise exceptions.

When we define our own class, object is the superclass. The following is an example class definition that simply extends object with a new name:

>>> class X: 
>>>     pass 

The following are some interactions with this tiny class definition:

>>> X.__class__ 
<class 'type'> 
>>> X.__class__.__base__ 
<class 'object'> 

We can see that a class is an object of the class named type and that the base class for our new class is the class named object. As we look at each method, we also take a look at the default behavior inherited from object. In some cases, the superclass special method's behavior will be exactly what we want. In other cases, we'll need to override the behavior of the special method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset