静的メソッドとクラスメンバーメソッド

899 ワード

スタティックメソッドとクラスメンバーメソッドは、それぞれ作成時にStaticmethodタイプとClassmethodタイプにロードされます.
の双曲線コサインを返します.
静的メソッドの定義にはselfパラメータがなく、クラス自体で直接呼び出すことができる.
クラスメソッドの定義にはclsというselfのようなパラメータが必要です.クラスメンバーメソッドはクラスの特定のオブジェクトを直接使用できます.
を呼び出します.しかしclsパラメータは自動的にクラスにバインドされます.

__metaclass__ = type
class MyClass:
    def smeth():
        print 'This is a static method'
    smeth = staticmethod(smeth)

    def cmeth(cls):
        print 'This is a class method of', cls
    cmeth = classmethod(cmeth)

装飾:

__metaclass__ = type
class MyClass:

    @staticmethod
    def smeth():
        print 'This is a static method'

    @classmethod
    def cmeth(cls):
        print 'This is a class method of', cls