为什么Django需要引入装饰器classonlymethod?为什么不能重用python classmethod?
mad*_*jar 43
最好的解释可能是源代码本身:
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)
Run Code Online (Sandbox Code Playgroud)
区别在于classmethod可以在实例上调用a,与在类上调用它具有相同的效果,但classonlymethod只能在类上调用.
| 归档时间: |
|
| 查看次数: |
4290 次 |
| 最近记录: |