Oop 告诉基类函数根据调用的子类执行不同的操作

Oop 告诉基类函数根据调用的子类执行不同的操作,oop,python-3.x,Oop,Python 3.x,在基类中,我有以下代码: @staticmethod def _split_on_empty_lines(file_contents): """generator that yields sections of a file separated by empty lines""" section = '' for line in file_contents: if line.strip(): section += line

在基类中,我有以下代码:

@staticmethod
def _split_on_empty_lines(file_contents):
    """generator that yields sections of a file separated by empty lines"""

    section = ''
    for line in file_contents:
        if line.strip():
            section += line
        else:
            _section_yield_function()
            section = ''
_section_yield_函数应该在这两个子类中做不同的事情。在第一种情况下,应检查空行后的行是否也是空的,然后再进行让步;在另一种情况下,它不应执行该检查


我了解到函数的调用类在函数上下文中是未知的,那么实现这种行为的好方法是什么?

您的staticmethod可以改为classmethod吗?或者甚至是一个实例方法?在子类而不是基类中实现它?如果行为因班级而异,你确定它应该是静态的吗?谢谢,我认为这解决了问题。我对OOP装饰程序不太自信,所以我忽略了@classmethod可以访问cls。