What is the difference between django classonlymethod and python classmethod?

Question:

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

Asked By: canadadry

||

Answers:

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.

Answered By: madjar
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.