By contract, every Python function returns something, even if it is the None
value, which can be returned implicitly by omitting the
return
statement, or explicitly.
The __init__
method is required to return None
. A TypeError
will be raised if the __init__
method either yields or returns any expression other than None
. While explicitly returning an expression that evaluates to
None
will not raise an error, it is considered bad practice.
To fix this issue, make sure that the __init__
method does not contain any return statement.
Code examples
Noncompliant code example
class MyClass(object):
def __init__(self):
self.message = 'Hello'
return self # Noncompliant: a TypeError will be raised
Compliant solution
class MyClass(object):
def __init__(self):
self.message = 'Hello'