Python Django';s ClassOnly方法有用吗?
源代码如下:Python Django';s ClassOnly方法有用吗?,python,django,Python,Django,源代码如下: 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
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)
虽然我可以看到classonlymethod只能在类上调用,而不能在与python的classmethod不同的实例上调用,但为什么我们需要这样的“限制”
在www上,关于classonlymethod和任何外行示例的内容并不多。它在内部用于基于类的视图“
as_view
,用于向试图在实例上调用它的人提供描述性错误消息
我不确定是谁第一次决定它是强制的。它在基于类的视图内部使用,作为\u视图向试图在实例上调用它的人提供描述性错误消息
我不知道是谁第一次决定它是强制性的。。。。我想不出一个。@dm3514不太可能。我看到了,但那只是来源,并没有回答我的问题。。。。我想不出一个。@dm3514不太可能。我看到了,但这只是一个来源,并没有回答我的问题。还有其他的答案说明了它的作用——但这是我看到的唯一说明原因的答案。这就是我想知道的。谢谢。还有其他的答案说明了它的作用——但这是我见过的唯一一个说明原因的答案。这就是我想知道的。谢谢