我正在尝试创建一个简单的动态插件系统,其中继承自类Bot
的插件会在导入时自动注册。
以下是我的代码。请注意,我正在使用this短文。
import os.path as osp
from glob import glob
from imp import load_source
from twisted.internet import reactor
run = reactor.run
halt = reactor.stop
class Reactor(object):
_bot_registry = {}
def __init__(self, botdir):
map(load_source, *enumerate(glob(osp.join(botdir, '../bots/*.py'))))
self.bots = {k: v() for k, v in self._bot_registry.iteritems()}
class Bot(object):
"""Base class for Bots"""
class __metaclass__(type):
def __init__(cls, name, bases, dict):
type.__init__(name, bases, dict)
Reactor._bot_registry[name] = cls
def _update(self):
"""Run through one scrape/process/respond iteration."""
pass
def scrape(self):
pass
def process(self):
pass
def publish(self):
pass
导入上面的代码时,我得到一个TypeError,如下所示:
TypeError Traceback (most recent call last) <ipython-input-42-dc3963f5e69b> in <module>()
19
20
---> 21 class Bot(object):
22 """Base class for Knacki Bots"""
23 class __metaclass__(type):
<ipython-input-42-dc3963f5e69b> in __init__(cls, name, bases, dict)
23 class __metaclass__(type):
24 def __init__(cls, name, bases, dict):
---> 25 type.__init__(name, bases, dict)
26 Reactor._bot_registry[name] = cls
27
TypeError: Error when calling the metaclass bases
descriptor '__init__' requires a 'type' object but received a 'str'
我做错了什么?
答案 0 :(得分:2)
type.__init__
与您的metaclass.__init__
具有相同的呼叫签名。第一个参数应为cls
:
type.__init__(cls, name, bases, dict)
顺便说一下:永远不要命名变量dict
,因为它会隐藏同名内置词。我建议
type.__init__(cls, name, bases, clsdict)