Summary

In this chapter, we have reviewed the various design alternatives of the __init__() method. The __init__() method is how objects are created, and it sets the initial state of an object.

We've looked at how all Python objects are subclasses of a common parent, the object class, and how the default __init__() method for the object class works. This consideration leads to two design strategies for placement of the __init__() method:

  • We can define a common __init__() method for all subclasses of a hierarchy. This can lead to using a factory function, separate from the __init__() method, to help initialize objects correctly.
  • We can push the __init__() method into each individual subclass of a complex hierarchy, and how this changes the design of classes.

After looking at building individual objects, we looked at how we can create composite objects. This involves a number of related uses of the __init__() methods of different classes. More advanced topics included defining stateless objects that don't need a sophisticated initialization, using class-level (or static) initialization, and how to validate values before creating an invalid object.

In the next chapter, we will take a look at special methods, along with a few advanced ones as well.

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

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