I'm riffing from the information here: Metaclass not being called in subclasses
My problem is that I'm unable to create an instance of an object using this class registry. If I use "regular" construction methods, then it seems to instantiate objects correctly; but when I try to use the class object associated with registry, then I get an error that I'm passing an incorrect number of arguments. (Seems to be calling the metaclass new and not my constructor...??)
I'm not clear why it's failing, because I thought I should be able to create an instance from the class object by using "callable" syntax.
Seems I'm getting the metaclass put in the registry and not the class itself? But I don't see an easy way to access the class itself in the new call.
Here is my code example, which fails to instantiate a variable 'd':
registry = [] # list of subclasses
class PluginMetaclass(type):
def __new__(cls, name, bases, attrs):
print(cls)
print(name)
registry.append((name, cls))
return super(PluginMetaclass, cls).__new__(cls, name, bases, attrs)
class Plugin(metaclass=PluginMetaclass):
def __init__(self, stuff):
self.stuff = stuff
# in your plugin modules
class SpamPlugin(Plugin):
def __init__(self, stuff):
self.stuff = stuff
class BaconPlugin(Plugin):
def __init__(self, stuff):
self.stuff = stuff
c = SpamPlugin(0)
b = BaconPlugin(0)
mycls = registry[1][1]
d = mycls(0)
Thanks for any help.