In order to be callable, a Python class should implement the __call__
method. Thanks to this method, an instance of this
class will be callable as a function.
However, when making a call to a non-callable object, a TypeError
will be raised.
In order to fix this issue, make sure that the object you are trying to call has a __call__
method.
Code examples
Noncompliant code example
class MyClass:
pass
myvar = MyClass()
myvar() # Noncompliant
none_var = None
none_var() # Noncompliant
Compliant solution
class MyClass:
def __call__(self):
print("called")
myvar = MyClass()
myvar()