1

我正在使用带有龙卷风的电机。我有以下课程:

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])

这实际上是我想要扩展的父类。子类会调用这个类的init来设置collectionName。问题是我在这个类中还有一些其他方法,例如

   @tornado.gen.coroutine
   def dropDB(self):
      yield self.xDb.drop_collection(self.COLLECTION??)

上面的内容被破坏了,因为我在init中动态设置了集合,这是我可以确定自我的一种方式。我设置为在基本方法中使用的变量?

4

1 回答 1

2

设置另一个变量:

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,但我展示的方式可能是最简单的。

http://motor.readthedocs.io/en/stable/api/motor_database.html#motor.motor_tornado.MotorDatabase.drop_collection

于 2016-06-25T13:45:23.723 回答