What is the difference between django classonlymethod and python classmethod?

前端 未结 1 1569
谎友^
谎友^ 2020-12-24 02:39

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

相关标签:
1条回答
  • 2020-12-24 02:49

    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.

    0 讨论(0)
提交回复
热议问题