django classonlymethod和python classmethod有什么区别?
发布于 2021-01-29 15:06:41
为什么Django需要引入装饰器classonlymethod
?为什么它不能重用python classmethod
?
关注者
0
被浏览
102
1 个回答
-
最好的解释是源代码本身:
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
只能在类上调用。