django classonlymethod和python classmethod有什么区别?

发布于 2021-01-29 15:06:41

为什么Django需要引入装饰器classonlymethod?为什么它不能重用python classmethod

关注者
0
被浏览
102
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    最好的解释是源代码本身:

    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)
    

    区别在于,classmethod可以在实例上调用a,与在类上调用具有相同的效果,但是classonlymethod只能在类上调用。



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看