python - How can I fix this metaclass-borne TypeError? -
i'm trying create simple dynamic plugin system in plugins inheriting class bot
automatically register on import.
below code far. please note working this short article.
import os.path osp glob import glob imp import load_source 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() k, v in self._bot_registry.iteritems()} class bot(object): """base class 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 1 scrape/process/respond iteration.""" pass def scrape(self): pass def process(self): pass def publish(self): pass
when importing code above, typeerror follows:
typeerror traceback (most recent call last) <ipython-input-42-dc3963f5e69b> in <module>() 19 20 ---> 21 class bot(object): 22 """base class 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 metaclass bases descriptor '__init__' requires 'type' object received 'str'
what have done wrong?
type.__init__
has same call signature metaclass.__init__
. first argument should cls
:
type.__init__(cls, name, bases, dict)
by way: never name variable dict
, since shadows builtin of same name. suggest
type.__init__(cls, name, bases, clsdict)
Comments
Post a Comment