-1

Where is the difference between this:

class Dummyclass:
    value = 4
    def dummymethod(value):
        Dummyclass.value = value

Dummyclass.dummymethod(31)

and:

class Dummyclass:
    value = 4
    @classmethod
    def dummymethod(cls, value):
        cls.value = value

Dummyclass.dummymethod(31)

Except there is a @classmethod?

deceze
  • 510,633
  • 85
  • 743
  • 889

1 Answers1

0

The differences are:

  • Without the decorator you can call DummyClass.dummymethod(42), but not DummyClass().dummymethod(42) (because the latter returns a bound method which tries to pass self as the first argument). With the decorator both will behave the same.
  • If you inherit this class and call dummymethod on a subclass, without decorator you'll always manipulate Dummyclass only; while with decorator you'll receive the subclass as argument instead and can thus implement proper inheritance.
deceze
  • 510,633
  • 85
  • 743
  • 889