Dynamic inheritance in Python

后端 未结 2 1942
攒了一身酷
攒了一身酷 2020-11-27 17:39

Say I have 2 different implementations of a class

class ParentA:
    def initialize(self):
        pass

    def some_event(self):
        pass

    def orde         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 18:07

    Simply store the class-object in a variable (in the example below, it is named base), and use the variable in the base-class-spec of your class statement.

    def get_my_code(base):
    
        class MyCode(base):
            def initialize(self):
              ...
    
        return MyCode
    
    my_code = get_my_code(ParentA)
    

提交回复
热议问题