【问题标题】:In Python, changing inherited behavior without rewriting the parent class在 Python 中,更改继承行为而不重写父类
【发布时间】:2019-05-07 20:02:25
【问题描述】:

在 Python 中,我有一个 ParentClass 类。它定义了方法 ParentClass.process() 等等。 ParentClass 由两个子类继承:ChildA 和 ChildB。这是系统的一部分,作为一个点轮。用户应该不能更改包的代码。

在执行过程中,系统会创建 ChildA 和 ChildB 的对象。

现在,我想让系统用户能够更改 ChildA 和 ChildB 对象的 .process() 方法的行为。

显然,她可以通过重新定义 ChildA.process() 和 ChildB.process() 来做到这一点,但这将复制相同的行为两次,总体而言,这似乎是一种非常奇怪的方法。

用户可以从 ParentClass 继承她自己的类并在那里重新定义 .process() 但这意味着我需要以某种方式动态地告诉 ChildA 和 ChildB 继承什么。

此外,所有类(ParentClass、ChildA 和 ChildB)都有其他方法,除了 .process(),需要保留而无需更改。

请帮助我找出最方便用户(可能也是最“pythonic”)的解决方法。

【问题讨论】:

    标签: python class oop inheritance


    【解决方案1】:

    您可以使用中间继承将公共覆盖定义为基类的扩展:

    class ParentClass:
        def process(self):
            print("ParentClass.process() called from",type(self))
    
    class ExtendedParent(ParentClass): 
        def process(self):
            super().process()
            print("Common Extension code for A and B",type(self))
    
    class ChildA(ExtendedParent):
        pass
    
    class ChildB(ExtendedParent):
        pass
    
    a = ChildA()
    b = ChildB()
    
    a.process()
    # ParentClass.process() called from <class '__main__.ChildA'>
    # Common Extension code for A and B <class '__main__.ChildA'>
    
    b.process()
    # ParentClass.process() called from <class '__main__.ChildB'>
    # Common Extension code for A and B <class '__main__.ChildB'>
    

    如果 ChildA 和 ChildB 在您的包中定义并且没有 .process() 方法,您可以指示您的包的用户使用不在任何类中的通用函数直接将方法分配给这些类:

    def extendedProcess(self):
        ParentClass.process(self) # this replaces super().process()
        print("Common Extension code for A and B",type(self))
    
    ChildA.process = extendedProcess        
    ChildB.process = extendedProcess
    

    如果 ChildA 和/或 ChildB 重写了 process() 并且您的用户想要更改父类中 process() 的行为,这是一种方法:

    ParentClass_process = ParentClass.process
    def extendedProcess(self):
        ParentClass_process(self) #call original code
        print("Common Extension code for A and B",type(self))    
    ParentClass.process = extendedProcess  
    

    【讨论】:

      【解决方案2】:

      您可以使用类方法为特定类重新分配process

      class ParentClass:
          def process(self):
              print("parent")
          @classmethod
          def reassign_process(cls, process):
              cls.process = process
      
      class ChildA(ParentClass):
          def process(self):
              print("Child A")
      
      class ChildB(ParentClass):
          def process(self):
              super().process()
              print("Child B")
      
      p = ParentClass()
      ca = ChildA()
      cb = ChildB()
      
      p.process()
      # parent
      ca.process()
      # Child A
      cb.process()
      # parent
      # Child B
      
      def new_parent_process(self):
          print("New parent")
      
      ParentClass.reassign_process(new_parent_process)
      
      def new_childa_process(self):
          print("New child A")
      
      ChildA.reassign_process(new_childa_process)
      
      p.process()
      # New parent
      ca.process()
      # New child A
      cb.process()
      # New parent
      # Child B
      

      【讨论】:

      • 您甚至不需要该方法,用户只需将函数分配给他们想要的任何类。
      • @kindall 是的,我想该方法真的存在,以便您可以编写有关它的文档。
      • 你可以把它变成一个属性;这样你至少可以检查以确保它是可调用的。
      猜你喜欢
      • 1970-01-01
      • 2014-01-10
      • 2013-07-11
      • 1970-01-01
      • 2021-05-30
      • 1970-01-01
      • 2017-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多