我有一个类'Collection',如下所示:
class Collection():
def __init__(self, db, collection_name):
self.db = db
self.collection_name = collection_name
if not hasattr(self.__class__, 'client'):
self.__class__.client = MongoClient()
self.data_base = getattr(self.client, self.db)
self.collection = getattr(self.data_base, self.collection_name)
def getCollectionKeys(self):
....etc.
我巧妙地创建了一个函数来创建类实例的字典,如下所示:
def getCollections():
collections_dict = {}
for i in range(len(db_collection_names)):
collections_dict[db_collection_names[i]] = Collection(database_name, db_collection_names[i])
return collections_dict
它有效。但是,每当我想访问一个类实例时,我都要浏览字典:
agents_keys = collections_dict['agents'].getCollectionKeys()
我很想写:
agents_keys = agents.getCollectionKeys()
是否有一种简单的方法可以将这些实例“排除”在dict之外?