18
为什么Django需要引入装饰器classonlymethod
? 为什么不能重复使用python classmethod
?django classonlymethod和python classmethod有什么区别?
为什么Django需要引入装饰器classonlymethod
? 为什么不能重复使用python classmethod
?django classonlymethod和python classmethod有什么区别?
最好的解释可以是源代码本身:
class classonlymethod(classmethod):
def __get__(self, instance, owner):
if instance is not None:
raise AttributeError("This method is available only on the view class.")
return super(classonlymethod, self).__get__(instance, owner)
不同的是,一个classmethod
可以被称为上的实例中,具有作为调用它的类相同的效果,但classonlymethod
只能在课堂上被召唤。
因此,'classonlymethod'和'staticmethod'之间的唯一区别在于它的签名吗? – arijeet
@ redskins80不,区别在于你*不能在类实例中使用'classonlymethod'。 – dotslash