Instance Method First Parameter Should Be self
¶
Sourcery refactoring id: instance-method-first-arg-name
¶
Description:¶
Suggests that instance methods should rename their first parameter to self
.
Before:¶
class Chicken:
def lay_egg(this, coop):
with this.enter_coop(coop):
coop.eggs.add(Egg(this.dna))
After:¶
class Chicken:
def lay_egg(self, coop):
with self.enter_coop(coop):
coop.eggs.add(Egg(self.dna))
Explanation:¶
This is both a very strong convention in Python and an explicit entry in PEP-8. Adhering to it improves code consistency and makes it easier for developers to understand the intention of your code.
Note that we do not suggest this where there are decorators present, since these can
override the behaviour, meaning that self
is not the correct name.
See also: class-method-first-arg-name