26
votes

Why is there a need for Django to introduce the decorator classonlymethod ? Why can't it reuse python classmethod?

1

1 Answers

50
votes

The best explanation is the source code itself:

class classonlymethod(classmethod):
    def __get__(self, instance, cls=None):
        if instance is not None:
            raise AttributeError("This method is available only on the class, not on instances.")
        return super().__get__(instance, cls)

The difference is that a classmethod can be called on an instance, having the same effect as calling it on the class, but the classonlymethod can only be called on the class.