How do I dynamically add mixins as base classes without getting MRO errors?

后端 未结 3 1053
轻奢々
轻奢々 2021-02-20 17:39

Say I have a class A, B and C.

Class A and B are both mixin classes for Class C.

         


        
相关标签:
3条回答
  • 2021-02-20 17:51

    Think of it this way -- you want the mixins to override some of the behaviors of object, so they need to be before object in the method resolution order.

    So you need to change the order of the bases:

    class C(A, B, object):
        pass
    

    Due to this bug, you need C not to inherit from object directly to be able to correctly assign to __bases__, and the factory really could just be a function:

    class FakeBase(object):
        pass
    
    class C(FakeBase):
        pass
    
    def c_factory():
        for base in (A, B):
            if base not in C.__bases__:
                C.__bases__ = (base,) + C.__bases__
        return C()
    
    0 讨论(0)
  • 2021-02-20 18:04

    I don't know the details, so maybe I'm completely off-base here, but it seems like you're using the wrong mechanisms to achieve your design.

    First off, why is Cfactory a class, and why does its __new__ method return an instance of something else? That looks like a bizarre way to implement what is quite naturally a function. Cfactory as you've described it (and shown a simplified example) doesn't behave at all like a class; you don't have multiple instances of it that share functionality (in fact it looks like you've made it impossible to construct instances of naturally).

    To be honest, C doesn't look very much like a class to me either. It seems like you can't be creating more than one instance of it, otherwise you'd end up with an ever-growing bases list. So that makes C basically a module rather than a class, only with extra boilerplate. I try to avoid the "single-instance class to represent the application or some external system" pattern (though I know it's popular because Java requires that you use it). But the class inheritance mechanism can often be handy for things that aren't really classes, such as your plugin system.

    I would've done this with a classmethod on C to find and load plugins, invoked by the module defining C so that it's always in a good state. Alternatively you could use a metaclass to automatically add whatever plugins it finds to the class bases. Mixing the mechanism for configuring the class in with the mechanism for creating an instance of the class seems wrong; it's the opposite of flexible de-coupled design.

    If the plugins can't be loaded at the time C is created, then I would go with manually invoking the configurator classmethod at the point when you can search for plugins, before the C instance is created.

    Actually, if the class can't be put into a consistent state as soon as it's created I would probably rather go for dynamic class creation than modifying the bases of an existing class. Then the system isn't locked into the class being configured once and instantiated once; you're at least open to the possibility of having multiple instances with different sets of plugins loaded. Something like this:

    def Cfactory(*args, **kwargs):
        plugins = find_plugins()
        bases = (C,) + plugins
        cls = type('C_with_plugins', bases, {})
        return cls(*args, **kwargs)
    

    That way, you have your single call to create your C instance with gives you a correctly configured instance, but it doesn't have strange side effects on any other hypothetical instances of C that might already exist, and its behaviour doesn't depend on whether it's been run before. I know you probably don't need either of those two properties, but it's barely more code than you have in your simplified example, and why break the conceptual model of what classes are if you don't have to?

    0 讨论(0)
  • 2021-02-20 18:06

    There is a simple workaround: Create a helper-class, with a nice name, like PluginBase. And use that the inherit of, instead of object.

    This makes the code more readable (imho) and it circumstances the bug.

    class PluginBase(object): pass
    class ServerBase(object): pass
    
    class pluginA(PluginBase): "Now it is clearly a plugin class"
    class pluginB(PluginBase): "Another plugin"
    
    class Server1(ServerBase, pluginA, pluginB): "This works"
    class Server2(ServerBase): pass
    Server2.__base__ += (PluginA,) # This also works
    

    As note: Probably you don't need the factory; it's needed in C++, but hardly in Python

    0 讨论(0)
提交回复
热议问题