The base class object __init__() method

Fundamental to the life cycle of an object are its creation, initialization, and destruction. We'll defer creation and destruction to a later chapter on more advanced special methods and focus on initialization. This will set the initial state of the object.

The superclass of all classes, object, has a default implementation of __init__() that amounts to pass. We aren't required to implement __init__(). If we don't implement it, then no instance variables will be created when the object is created. In some cases, this default behavior is acceptable.

We can add attributes to an object that's a subclass of object. Consider the following class, which requires two instance variables, but doesn't initialize them:

class Rectangle: 
    def area(self) -> float: 
        return self.length * self.width 

The Rectangle class has a method that uses two attributes to return a value. The attributes have not been initialized anywhere in the class definition. While this is legal Python, it's a little strange to avoid specifically setting attributes. The following is an interaction with the Rectangle class:

>>> r = Rectangle() 
>>> r.length, r.width = 13, 8
>>> r.area()
104

While this is legal, it's a potential source of deep confusion, which is a good reason to avoid it. Setting ad-hoc attribute values outside the class body in the example shown above defeats type hint checking by mypy, which is another reason for avoiding it.

This kind of design grants some flexibility, so there could be times when we needn't set all of the attributes in the __init__() method. We walk a fine line here. An optional attribute implies a kind of subclass that's not formally declared as a proper subclass.

We're creating polymorphism in a way that could lead to confusing and inappropriate use of convoluted if statements. While uninitialized attributes may be useful, they could be a symptom of bad design.

The Zen of Python, by Tim Peters, available from the standard library via import this, offers the following advice:

"Explicit is better than implicit."

This statement has proven helpful over the years, to help keep Python programs simple and consistent. This is Python Enhancement Proposal (PEP) number 20. See https://www.python.org/dev/peps/pep-0020/ for further information.

An __init__() method should make instance variables explicit.

Pretty poor polymorphism
There's a fine line between flexibility and foolishness. We may have stepped over the edge of flexible into foolish as soon as we feel the need to write the following:

if 'x' in self.__dict__:
    code-to-handle-optional-attribute

Or, we could see the following:

try:
    self.x
except AttributeError:
    code-to-handle-optional-attribute

It's time to reconsider the API and add a common method or attribute. Refactoring is better than adding if statements.
..................Content has been hidden....................

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