Instance methods, i.e. methods not annotated with @classmethod
or @staticmethod
, are expected to have at least one
parameter. This parameter will reference the object instance on which the method is called. By convention, this first parameter is named "self".
Naming the first parameter something different from "self" is not recommended as it could lead to confusion. It might indicate that the "self"
parameter was forgotten, in which case calling the method will most probably fail.
Note also that creating methods which are used as static methods without the @staticmethod
decorator is a bad practice. Calling these
methods on an instance will raise a TypeError
. Either move the method out of the class or decorate it with
@staticmethod
.
Exceptions
This rule will accept "cls" or "mcs" as first parameter’s name for class and metaclasses methods.
No issue will be raised for the following methods: __init_subclass__
, __class_getitem__
and __new__
, as
these methods' first parameter is a class.
This rule may be parameterized to prevent raising issues on methods decorated with specific decorators. These decorators can be added to this
rule’s ignoredDecorators
parameter.
For example, with ignoredDecorators
set to "myDecorator".
class MyClass(ABC):
@myDecorator
def method(arg): # No issue will be raised.
pass