2011-11-15 86 views

回答

37

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

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只能在课堂上被召唤。

+0

因此,'classonlymethod'和'staticmethod'之间的唯一区别在于它的签名吗? – arijeet

+1

@ redskins80不,区别在于你*不能在类实例中使用'classonlymethod'。 – dotslash