django classonlymethod和python classmethod有什么区别? [英] What is the difference between django classonlymethod and python classmethod?

查看:263
本文介绍了django classonlymethod和python classmethod有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么Django需要介绍装饰器 classonlymethod
为什么不能重用python classmethod

Why is there a need for Django to introduce the decorator classonlymethod ? Why can't it reuse python classmethod?

推荐答案

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

The best explanation may be the source code itself :

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

The difference is that a classmethod can be called on an instance, having the same effect as calling it on the class, but the classonlymethod can only be called on the class.

这篇关于django classonlymethod和python classmethod有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆