Python:在抽象类中动态调用函数
我有一个类实例需要调用analysis方法。构造函数接收带有此类消息的请求对象 { “信息”:{ “属性”:{ “消息id”:“2aj78h98-2112-4637-76h1-09ec727933bb”, “主题”:“我的主题”, “版本”:1 }, “数据”:“{”id:123541234}” } } MyModel类的结构是:Python:在抽象类中动态调用函数,python,python-2.7,abstract-class,Python,Python 2.7,Abstract Class,我有一个类实例需要调用analysis方法。构造函数接收带有此类消息的请求对象 { “信息”:{ “属性”:{ “消息id”:“2aj78h98-2112-4637-76h1-09ec727933bb”, “主题”:“我的主题”, “版本”:1 }, “数据”:“{”id:123541234}” } } MyModel类的结构是: import importlib class MyModel(object): def __init__(self, req): ""
import importlib
class MyModel(object):
def __init__(self, req):
"""
From the request, extract the topic and dynamically create is instance
"""
try:
self.req = req
classname = self.req.topic # in this example: MyTopic
classpath = 'foo.bar'
module = importlib.import_module(classpath)
self.some_object = getattr(module, classname)
except Exception as e:
raise Exception(e.message)
def analyse(self):
data = self.req.data
# self.some_object is an instance of foo.bar.MyTopic
self.some_object.analyse(data)
下面是MyTopic类的结构:
class MyTopic(TopicBase):
def __init__(self):
super(MyTopic, self).__init__()
self.retreive_mode = 'api'
def _retreive_from_db(self):
print 'in db'
def _retreive_from_datawarehouse(self):
print 'in datawarehouse'
def _retreive_from_api(self):
print 'in api
及其父类:
from abc import ABCMeta, abstractmethod
class TopicBase(object):
__metaclass__ = ABCMeta
def __init__(self, *args, **kwargs):
pass
def analyse(self, data):
"""
Base on the class configuration, call the proper retreive method
"""
def func_not_found(): # just in case we dont have the method
raise NotImplementedError
func_name = '_retreive_from_' + self.retreive_mode
func = getattr(self, func_name, func_not_found)
func(data)
@abstractmethod
def _retreive_from_db(self):
pass
@abstractmethod
def _retreive_from_datawarehouse(self):
pass
@abstractmethod
def _retreive_from_api(self):
pass
调用我的_model.analysis(data)时,我得到错误:
TypeError: unbound method analyse() must be called with MyTopic instance as first argument (got dict instance instead)
TypeError: unbound method analyse() must be called with MyTopic instance as first argument (got ABCMeta instance instead)
my_模型已经是MyTopic的一个实例。因此,如果我尝试我的_模型。分析(我的_模型,数据),我会得到错误:
TypeError: unbound method analyse() must be called with MyTopic instance as first argument (got dict instance instead)
TypeError: unbound method analyse() must be called with MyTopic instance as first argument (got ABCMeta instance instead)
有什么想法吗???这个
self.some_object = getattr(module, classname)
将self.some\u object
设置为类(不是该类的实例),这意味着当MyModel.analysis()调用
self.some_object.analyse(data)
它调用的是SomeClass.analysis(data)
,而不是SomeClass().analysis(data)
self.some_object = getattr(module, classname)
将self.some\u object
设置为类(不是该类的实例),这意味着当MyModel.analysis()调用
self.some_object.analyse(data)
它调用的是SomeClass.analysis(data)
而不是SomeClass().analysis(data)
您调用的是类而不是实例上的方法,因此必须将实例作为第一个参数传递。您实际上没有显示代码,但我猜您正在某处执行my_model=MyTopic
,而不是my_model=MyTopic()
,因此没有实际实例化类。您是在类上调用方法,而不是在实例上调用方法,因此必须将实例作为第一个参数传递。您实际上没有显示代码,但我猜您是在某处执行my_model=MyTopic
而不是my_model=MyTopic()
,因此实际上没有实例化该类。