Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何正确地修饰python中要重写的函数?_Python_Design Patterns_Python Decorators - Fatal编程技术网

如何正确地修饰python中要重写的函数?

如何正确地修饰python中要重写的函数?,python,design-patterns,python-decorators,Python,Design Patterns,Python Decorators,我想为重写指定一些基类函数。(不必强制执行覆盖。)这样做的正确方式是什么 我是说 class Base(object): ... @abstractmethod def fun(self): pass class Derived(Base): @override def fun(self): pass 编辑: 如果函数未被重写,我希望我的代码引发UnimplementedException class Base(obj

我想为重写指定一些基类函数。(不必强制执行覆盖。)这样做的正确方式是什么

我是说

class Base(object):
    ...
    @abstractmethod
    def fun(self):
        pass

class Derived(Base):

    @override
    def fun(self):
        pass
编辑:

如果函数未被重写,我希望我的代码引发UnimplementedException

class Base(object):
    ...
    def fun(self):
        raise NotImplementedError
资料来源:


来源:

你这是什么意思:不必强制执行覆盖。如果不重写,你想做什么?如果你不想重写一个方法,你不应该将它声明为抽象的,并提供一个基本的(甚至是空的)实现…谢谢你的问题,更新了OP。你这样说是什么意思:不必强制执行重写。如果不重写,你想做什么?如果你不想重写一个方法,你不应该将它声明为抽象的,并提供一个基本的(甚至是空的)实现…谢谢你的提问,更新了OP。