Skip to content

Class Method First Parameter Should Be `cls`

Documentation Index

Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt

Use this file to discover all available pages before exploring further.

Sourcery refactoring id: class-method-first-arg-name

Section titled “Sourcery refactoring id: class-method-first-arg-name”

Suggests that class methods should rename their first parameter to cls.

class Chicken:
@classmethod
def from_egg(new, egg):
with egg.hatch() as chick:
return new(chick)
class Chicken:
@classmethod
def from_egg(cls, egg):
with egg.hatch() as chick:
return cls(chick)

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.

See also instance-method-first-arg-name