0
我正在嘗試創建一個簡單的動態插件系統,其中繼承自類Bot
的插件在導入時自動註冊自己。我該如何解決這個元類傳輸的TypeError?
以下是我的代碼到目前爲止。請注意,我正在從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 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'
我做了什麼錯?
確實你是對的。我感到很傻。我會盡快接受! – blz 2013-05-13 19:09:44