In Flask-PyMongo, they use self._Collection__database to represent the database object that this Collection object belongs to:
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
Why is self._Collection__database not self.__database?
test <a>and <i>
Flask-PyMongo is not arbitrarily choosing that name.
The name is the result of name mangling:
In the parent-class definition, the attribute is defined as
self.__databaseand Python “mangles” the name (toself._ClassName__attributename) so that any subclasses do not overwrite with their own assignment to their ownself.__databaseattribute.