You are probably using Python’s __init__ method incorrectly


 Python is an object oriented language. How a new object should be created is usually defined in the special __init__ method implemented on a class. A simple class that stores two instance variables could be implemented as follows:

Creating an object follows the syntax <classname>(<arguments passed to __init__>). In this case, the __init__ method takes in two arguments which are stored as instance variables. Once the object is created, methods can be called on it that use this data.

However, most objects are much more complicated to build. Often the data that the object needs to store is not readily available and needs to be derived from some other input. Often we want to be able to create the same object from different kinds of input.

The mistake I see in many Python code bases is that all this logic is layered into the __init__ method. The fact that everything happens in __init__ may be obfuscated by some auxiliary _initialize method, but the result is the same: the creation logic for Python objects become incomprehensible monstrosities.

Let’s look at an example. We have an object that represents some collection of configuration, which we typically load from a file. I’ve seen such a class be implemented as follows:

Click