在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
工作正常。虽然我想定义另一个
class CollectionV(collection.Collection)
类由于某种原因(原因:我想让'remove'操作只将字段值从“visible”更改为“removed”),类定义为:
class CollectionV(collection.Collection):
"""Custom sub-class of :class:`pymongo.collection.Collection` which
adds Flask-specific helper methods.
"""
def __getattr__(self, name):
attr = super(CollectionV, self).__getattr__(name)
if isinstance(attr, collection.Collection):
db = self._CollectionV__database
return CollectionV(db, attr.name)
return attr
然后我收到'RuntimeError: maximum recursion depth exceeded'
错误,错误日志显示"self._CollectionV__database"
被视为一个集合,它会继续以递归方式调用__getattr__()
。
为什么在"self._Collection__database"
中使用class Collection(collection.Collection)
是正常的,而CollectionV
会导致问题?