2012-03-25 8 views
2

は、彼らがこのコレクションオブジェクトが属するデータベースオブジェクトを表すためにself._Collection__databaseを使用します。フラスコ - PyMongo:どうして "self._Collection__database"?フラスコ-PyMongoで

class Collection(collection.Collection): 
    """Custom sub-class of :class:`pymongo.collection.Collection` which 
    adds Flask-specific helper methods. 
    """ 

    def __getattr__(self, name): 
    attr = super(Collection, self).__getattr__(name) 
    if isinstance(attr, collection.Collection): 
     db = self._Collection__database 
     return Collection(db, attr.name) 
    return attr 

なぜself._Collection__databaseないself.__databaseのですか?

test <a>and <i> 

答えて

2

Flask-PyMongoは、その名前を任意に選択していません。
名前がname manglingの結果である:クラスのプライベートメンバーのための有効なユースケースがありますので、(つまり にサブクラスで定義された名前と名前の名前の衝突を避けるため)

が が限定的にサポートされますネームマングリングと呼ばれるそのようなメカニズムのために。任意の 形式の識別子__spam(少なくとも2つの先頭のアンダースコア、 の末尾に1つの末尾のアンダースコア)は、テキストで、 _classname__spamに置き換えられます。classnameは、先頭のアンダースコアを取り除いた現在のクラス名です。 the parent-class definition

任意のサブクラスが自身のself.__database属性に自分の割り当てを上書きしないように、属性がself.__databaseと(self._ClassName__attributenameに)Pythonの「マングル」名として定義されます。