18
Djangoがデコレータclassonlymethod
を導入する必要があるのはなぜですか? なぜPython classmethod
を再利用できないのですか?django classonlymododとpython classmethodの違いは何ですか?
Djangoがデコレータclassonlymethod
を導入する必要があるのはなぜですか? なぜPython classmethod
を再利用できないのですか?django classonlymododとpython classmethodの違いは何ですか?
最良の説明は、ソースコードそのものであってもよい。
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
のみ缶クラスで呼び出される。
したがって、 'classonlymethod'と' staticmethod'の唯一の違いは署名にありますか? – arijeet
@ redskins80いいえ、違いはクラスインスタンスで 'classonlymethod'を使用できないことです。 – dotslash