Skip to content

Class Method First Parameter Should Be cls

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


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


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


class Chicken:
    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