最好的解释是源代码本身:
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
只能在类上调用。