[英]Python: Dynamically call function within abstract class
我有一个需要调用分析方法的类实例。 构造函数通过这种消息接收请求对象
{“ message”:{“ attributes”:{“ message_id”:“ 2aj78h98-2112-4637-76h1-09ec727933bb”,“ topic”:“ MyTopic”,“ version”:1},“ data”:“ {” id “:123541234}”}}
my_model = MyModel(request)
my_model.analyse(data)
MyModel类的结构为:
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
调用my_model.analyse(data)时,出现错误:
TypeError: unbound method analyse() must be called with MyTopic instance as first argument (got dict instance instead)
my_model已经是MyTopic的实例。 因此,如果我尝试使用my_model.analyse(my_model,data),则会收到错误消息:
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_object
设置为一个类(而不是该类的实例),这意味着当MyModel.analyse()
调用时
self.some_object.analyse(data)
它在调用SomeClass.analyse(data)
而不是SomeClass().analyse(data)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.