我正在学习python,我正面临两难选择:
from abc import ABCMeta, abstractproperty, abstractmethod
import jsocket
from acme.core import StatusCode, Direction
import acme.db as db
class ModuleThread(jsocket.ServerFactoryThread):
__metaclass__ = ABCMeta
@abstractproperty
def module_name(self):
pass
@abstractproperty
def _db_model(self):
pass
def __init__(self):
super(ModuleThread, self).__init__()
self.code = StatusCode.PENDING
self.status = None
def _get_config_for_domain(self, domain, direction):
# here I want to be sure that my db model is an instance
# of a pewee model
print self._db_model
class CheckMxThread(ModuleThread):
@property
def module_name(self):
return 'check_mx'
@property
def _db_model(self):
return db.ModMx
和电话
CheckMxThread()._get_config_for_domain('nocheck.mx', Direction.INCOMING)
我想确保在使用self._db_model
时获得Pewee Model的实例,我该如何处理:
db
包,稍后在子类中使用self.db.ModMx
?答案 0 :(得分:0)
您可以使用isinstance
:
@property
def _db_model(self):
if not instanceof(self, db.ModMx):
raise IOError('can not use this instance type')
这项工作涉及儿童入学,例如:
>>> class A:
... pass
>>> class B(A):
... pass
>>> b = B()
>>> isinstance(b, A)
True