__init__ method returns a value¶
ID: py/explicit-return-in-init Kind: problem Severity: error Precision: very-high Tags: - reliability - correctness Query suites: - python-security-and-quality.qls
__init__ method of a class is used to initialize new objects, not create them. As such, it should not return any value. Returning
None is correct in the sense that no runtime error will occur, but it suggests that the returned value is meaningful, which it is not.
return expr statement to a plain
return statement, or omit it altogether if it is at the end of the method.
In this example, the
__init__ method attempts to return the newly created object. This is an error and the return method should be removed.
class ExplicitReturnInInit(object): def __init__(self, i): self.i = i return self
Python: The init method.