相关疑难解决方法(0)

Python中的动态继承

假设我有两个不同的类实现

class ParentA:
    def initialize(self):
        pass

    def some_event(self):
        pass

    def order(self, value):
        # handle order in some way for Parent A


class ParentB:
    def initialize(self):
        pass

    def some_event(self):
        pass

    def order(self, value):
        # handle order in another for Parent B
Run Code Online (Sandbox Code Playgroud)

我怎样才能动态地让一些第三类继承ParentAParentB基于这样的东西?

class MyCode:
    def initialize(self):
        self.initial_value = 1

    def some_event(self):
        # handle event
        order(self.initial_value)


# let MyCode inherit from ParentA and run
run(my_code, ParentA)
Run Code Online (Sandbox Code Playgroud)

python inheritance

26
推荐指数
2
解决办法
1万
查看次数

标签 统计

inheritance ×1

python ×1