- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
在Flask-PyMongo中,他们使用self._Collection__database
来表示这个Collection对象所属的数据库对象:
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>
最佳答案
Flask-PyMongo 并不是随意选择的名称。
名称是 name mangling 的结果:
Since there is a valid use-case for class-private members (namely to avoid name clashes of names with names defined by subclasses), there is limited support for such a mechanism, called name mangling. Any identifier of the form
__spam
(at least two leading underscores, at most one trailing underscore) is textually replaced with_classname__spam
, where classname is the current class name with leading underscore(s) stripped.
在the parent-class definition ,该属性被定义为 self.__database
并且 Python“破坏”名称(到 self._ClassName__attributename
),这样任何子类都不会被自己的赋值覆盖self.__database
属性。
关于python - Flask-PyMongo : how come "self._Collection__database"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9857895/
在Flask-PyMongo中,他们使用self._Collection__database来表示这个Collection对象所属的数据库对象: class Collection(collection
我是一名优秀的程序员,十分优秀!