In Python can one implement mixin behavior without using inheritance?

前端 未结 8 1504
醉梦人生
醉梦人生 2021-02-06 13:10

Is there a reasonable way in Python to implement mixin behavior similar to that found in Ruby -- that is, without using inheritance?

class Mixin(object):
    def         


        
8条回答
  •  爱一瞬间的悲伤
    2021-02-06 13:31

    def mixer(*args):
        """Decorator for mixing mixins"""
        def inner(cls):
            for a,k in ((a,k) for a in args for k,v in vars(a).items() if callable(v)):
                setattr(cls, k, getattr(a, k).im_func)
            return cls
        return inner
    
    class Mixin(object):
        def b(self): print "b()"
        def c(self): print "c()"
    
    class Mixin2(object):
        def d(self): print "d()"
        def e(self): print "e()"
    
    
    @mixer(Mixin, Mixin2)
    class Foo(object):
        # Somehow mix in the behavior of the Mixin class,
        # so that all of the methods below will run and
        # the issubclass() test will be False.
    
        def a(self): print "a()"
    
    f = Foo()
    f.a()
    f.b()
    f.c()
    f.d()
    f.e()
    print issubclass(Foo, Mixin)
    

    output:

    a()
    b()
    c()
    d()
    e()
    False
    

提交回复
热议问题