2012-03-29 11 views
4

私は、使用している数字とデータの種類の処理関数を含む共通のモジュールを持っています。 from common import common(またはそれ以上の場合はただちにimport common)のように含めることができます。common.howLongAgo(unixTimeStamp)Pythonでは、インスタンスを渡さずにクラスメソッドを使用するにはどうすればよいですか?

共有モジュールでこれを行うには、何が必要ですか? Commonはクラス 'common'からなるモジュールです。インポート、今

def module_method(): 
    return "I am a module method" 

class ModClass: 
    @staticmethod 
    def static_method(): 
     # the static method gets passed nothing 
     return "I am a static method" 
    @classmethod 
    def class_method(cls): 
     # the class method gets passed the class (in this case ModCLass) 
     return "I am a class method" 
    def instance_method(self): 
     # An instance method gets passed the instance of ModClass 
     return "I am an instance method" 

:あなたは、クラスメソッドは、より便利にしたい場合は

>>> import foo 
>>> foo.module_method() 
'I am a module method' 
>>> foo.ModClass.static_method() 
'I am a static method' 
>>> foo.ModClass.class_method() 
'I am a class method' 
>>> instance = ModClass() 
>>> instance.instance_method() 
'I am an instance method' 

は、インポート

モジュールfoo.py:Pythonのモジュールでメソッドを公開するの

+5

なぜこれらの機能がクラスの最初の場所にあるのですか?彼らが 'self'を必要としなければ、なぜモジュールスコープに移動させないのですか? –

+1

メインのPythonファイル – Supernovah

+0

を取得するには、これを達成するためにクラスに入れる必要はありません。それらをモジュールに移動し、モジュールレベルにしておきます。 –

答えて

25

つの方法直接:

>>> from foo import ModClass 
>>> ModClass.class_method() 
'I am a class method' 

またimport ... as ...それをより読みやすくすることができます:あなたが使うべきもの

>>> from foo import ModClass as Foo 
>>> Foo.class_method() 
'I am a class method' 

はやや好みの問題です。一般的に、コレクションのようなものに作用する、またはいくつかの計算を実行したり、いくつかのリソースを取得シンプルなユーティリティ関数がクラスに関連

  • 機能モジュール方式でなければなりません

    • が、それはどちらかは必要ありません:親指の私の個人的なルールがありますクラスまたはインスタンスは静的メソッドである必要があります
    • クラスに関連し、比較のためにクラスを必要とするか、またはクラス変数にアクセスする関数はクラスメソッドでなければなりません。
    • インスタンスに対して動作する関数はインスタンスメソッドでなければなりません。あなたは、モジュールcommon.pyと機能を持っている場合
  • +0

    このようなモジュールスコープでメソッドを公開するのは、一般的なコードのプラクティスですか?それにもかかわらず、それはとても感謝しました。 – Supernovah

    +1

    '@ classmethod'を使ってクラスメソッドをデコレートすることは、きれいです – Daenyth

    +0

    これで十分です。標準ライブラリにはいくつかの例があります。 'os'モジュールは1つです。 – brice

    2

    がクラスに共通

    class common(object): 
        def howLongAgo(self,timestamp): 
          some_code 
    

    であるあなたは、静的メソッドの聖霊降臨祭のデコレータであるためにあなたの方法を変更する必要があります。この方法であなた

    class common(object): 
        @staticmethod 
        def howLongAgo(timestamp): # self goes out 
          some_code 
    

    を@staticmethodクラス全体を変更する必要はなく、クラス内でself.howLongAgoを使用することもできます

    0
    class Test(object): 
        @classmethod 
        def class_method(cls): 
         return "class method" 
    
    test = Test() 
    print test.class_method() 
    
    [[email protected] scar]$ python ~/test.py 
    class method 
    

    これは、古いスタイルのクラスではなく、現代のPythonオブジェクトを使ってクラスメソッドを行う標準的な方法です。

    代わりに、あなたはstaticmethodを意味するかもしれません:あなたの問題のためのあなたに、より理にかなっている方

    class Test(object): 
        @staticmethod 
        def static_method(): 
         return "static method" 
    
    test = Test() 
    print test.static_method() 
    [[email protected] scar]$ python ~/test.py 
    static method 
    

    使用。静的メソッドは、通常、独自の独立した機能に分離する必要があります、クラスの使用は余分です。

    Cf. http://pyvideo.org/video/880/stop-writing-classes

    +0

    はい、問題は 'test = Test()'を実行してインスタンスを作成していることです。これは私が避けたいものです。 – Supernovah

    関連する問題