Skip to content

Class Method First Parameter Should Be cls

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

Description:

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

Before:

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

After:

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

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.

See also instance-method-first-arg-name

Back to top