__init__
method returns a value¶
ID: py/explicit-return-in-init
Kind: problem
Security severity:
Severity: error
Precision: very-high
Tags:
- reliability
- correctness
Query suites:
- python-security-and-quality.qls
Click to see the query in the CodeQL repository
The __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.
Recommendation¶
Convert the return expr
statement to a plain return
statement, or omit it altogether if it is at the end of the method.
Example¶
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
References¶
Python: The init method.