classmethod可以被一个实例调用, 但是classonlymethod不能,它只能被类调用.
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)
The difference is that a classmethod can be an instance, having the same effect as calling it on the class, but the classonlymethod can only be called on the class.
class Kls(object): no_inst = 0 def __init__(self): Kls.no_inst = Kls.no_inst + 1 @classmethod def get_no_of_instance(cls_obj): return cls_obj.no_inst ik1 = Kls() ik2 = Kls() print ik1.get_no_of_instance() print Kls.get_no_of_instance()