NoEmptyInit
Misconception:
It is not okay for a class to have an __init__
with an empty body.
Incorrect
__init__ must do something
Correct
The body of __init__ can be empty
CorrectionHere is what's right.
Here is what's right.
The following class is perfectly valid, although not useful:
class Device:
def __init__(self):
pass
What happens when executing an empty __init__
?
Despite the above __init__
of class Device
being empty, the Python interpreter will simply call this method after instantiating the object. While having an empty __init__
is useless, it is allowed in the language.
But doesn’t it have to return the object?
The method __init__
should not have an explicit return: although having a return statement in the __init__
is valid, it is not meaningful. It is just a default method that gets invoked upon object instantiation, which is usually used to set up instance variables and other operations.
Language
Python