在构造函数中动态初始化数据库
Dynamic initialisation of DB in constructor
我正在使用带龙卷风的电机。我有以下 class:
class N():
def __init__(self,collectionName host='localhost', port=27017):
self.con=motor.MotorClient(host,port)
self.xDb=self.con.XDb
setattr(self,collectionName,self.xDb[collectionName])
这实际上是我要扩展的 parent class。 child class 将调用此 class' init 来设置 collectionName。问题是我在这个 class 中还有一些其他方法,例如
@tornado.gen.coroutine
def dropDB(self):
yield self.xDb.drop_collection(self.COLLECTION??)
上面的内容被破坏了,因为我在 init 中动态设置了集合,我可以通过什么方式确定自己。我设置为在基本方法中使用的变量?
设置另一个变量:
class N():
def __init__(self, collectionName, host='localhost', port=27017):
# ... your existing code ...
self.collectionName = collectionName
@tornado.gen.coroutine
def dropDB(self):
yield self.xDb.drop_collection(self.collectionName)
由于 drop_collection
采用名称或 MotorCollection 对象,您可以通过其他方式将此数据存储在 self
上,但我展示的方式可能是最简单的。
我正在使用带龙卷风的电机。我有以下 class:
class N():
def __init__(self,collectionName host='localhost', port=27017):
self.con=motor.MotorClient(host,port)
self.xDb=self.con.XDb
setattr(self,collectionName,self.xDb[collectionName])
这实际上是我要扩展的 parent class。 child class 将调用此 class' init 来设置 collectionName。问题是我在这个 class 中还有一些其他方法,例如
@tornado.gen.coroutine
def dropDB(self):
yield self.xDb.drop_collection(self.COLLECTION??)
上面的内容被破坏了,因为我在 init 中动态设置了集合,我可以通过什么方式确定自己。我设置为在基本方法中使用的变量?
设置另一个变量:
class N():
def __init__(self, collectionName, host='localhost', port=27017):
# ... your existing code ...
self.collectionName = collectionName
@tornado.gen.coroutine
def dropDB(self):
yield self.xDb.drop_collection(self.collectionName)
由于 drop_collection
采用名称或 MotorCollection 对象,您可以通过其他方式将此数据存储在 self
上,但我展示的方式可能是最简单的。