Why use classmethod instead of staticmethod? [duplicate]

There’s little difference in your example, but suppose you created a subclass of Foo and called the create_new method on the subclass…

class Bar(Foo):
    pass

obj = Bar.create_new()

…then this base class would cause a new Bar object to be created…

class Foo:
    @classmethod
    def create_new(cls):
        return cls()

…whereas this base class would cause a new Foo object to be created…

class Foo:
    @staticmethod
    def create_new():
        return Foo()

…so the choice would depend which behavior you want.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)