python:如果只是导入而不是运行模块,如何避免非implementedError?

2024-09-30 22:24:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个类似接口的函数,它是这样实现的:

# File: parsers.py
class BaseParser(object):
    '''Interface class'''
    def __init__(self):
        self.content

    def Parse(self):
        """The interface, not implemented"""

        raise NotImplementedError('{}.Parse()'.format(inspect.stack()[0][3]))

class SimpleParser(BaseParser):
    '''Implementation class'''
    def __init__(self):
        BaseParser.__init__(self)

    def Parse(self):
        # Do real work

现在,当我导入这个模块时,我马上得到NotImplementedError,所以我不能使用SimpleParser。 我怎样才能使用这个异常习语并且仍然能够使用它呢?你知道吗

谢谢!你知道吗


Tags: 函数pyselfobjectparseinitdefcontent