2016-08-16 1 views
3

私はHTTP APIを構築しており、オブジェクトのコレクションに対する要求を処理するスーパークラスに多くのコードを組み込みました。私のサブクラスでは、操作が動作するデータベースモデルを指定し、スーパークラスは残りの部分を処理します。継承時にドキュメントストリングを変更しますが、メソッドを同じままにします

これは、get、postなどのメソッドをスーパークラスから再実装する必要はないことを意味しますが、サブクラスのdocstringを変更して、エンドポイントが実際に動作しているモデル。

親クラスの機能を継承するが、ドキュメントストリングを変更する最もクリーンな方法は何ですか?

例:

class CollectionApi(Resource): 
    """Operate on a collection of something. 
    """ 
    class Meta(object): 
     model = None 
     schema = None 


    def get(self): 
     """Return a list of collections. 
     """ 
     # snip 

    def post(self): 
     """Create a new item in this collection. 
     """ 
     # snip 

class ActivityListApi(CollectionApi): 
    """Operations on the collection of Activities. 
    """ 
    class Meta(object): 
     model = models.Activity 
     schema = schemas.ActivitySchema 

は具体的に、私はCollectionApiのようgetpost実行を持っているActivityListApiが必要ですが、私は(自動ドキュメンテーションのために)別のドキュメンテーション文字列をしたいです。

def get(self): 
     """More detailed docs 
     """ 
     return super(ActivityListApi, self).get() 

をしかし、これは厄介なようだ:

私はこれを行うことができます。

+2

私は方法が同じことをすれば、なぜ彼らは別のドキュメンテーション文字列が必要なのか、それを理解していませんか?あるいは、別の言い方をすると、基本クラスのユーザーは拡張されたドキュメント文字列の恩恵を受けることはありませんか? – fjarri

+0

これはAPIなので、子クラスが持っていると便利だと思いました。リクエストとレスポンスの例。特定のリクエストとレスポンスは、どのリソースが操作されているかによって異なりますので、そのような詳細を基本クラスに持つことはできません。 – Plasma

+0

詳細をdocstringクラスに追加します。 – ChrisC73

答えて

1
class CollectionApi(Resource): 
    """Operate on a collection of something. 
    """ 

    def _get(self): 
     """actual work... lotsa techy doc here! 

      the get methods only serve to have something to hang 
      their user docstrings onto 
     """ 

     pass 

    def get(self): 
     """user-intended doc for CollectionApi""" 
     return self._get() 

class ActivityListApi(CollectionApi): 

    def get(self): 
     """user-intended doc for ActivityListApi""" 
     return self._get() 
関連する問題