Home>Article>Backend Development> How to use NotImplementedError in Python programming_python

How to use NotImplementedError in Python programming_python

不言
不言 Original
2018-04-21 14:28:39 12668browse

下面为大家分享一篇Python编程中NotImplementedError的使用方法,具有很好的参考价值,希望对大家有所帮助。一起过来看看吧

Python编程中raise可以实现报出错误的功能,而报错的条件可以由程序员自己去定制。在面向对象编程中,可以先预留一个方法接口不实现,在其子类中实现。

如果要求其子类一定要实现,不实现的时候会导致问题,那么采用raise的方式就很好。

而此时产生的问题分类是NotImplementedError。

写一段代码如下:

class ClassDemo: def test_demo(self): raiseNotImplementedError("my test: not implemented!") classChildClass(ClassDemo): pass inst =ChildClass() inst.test_demo()

程序运行结果:

E:\01_workspace\02_programme_language\03_python\OOP\2017\08\10>pythonerror_demo.py Traceback (mostrecent call last): File "error_demo.py", line 9, in inst.test_demo() File "error_demo.py", line 3, intest_demo raise NotImplementedError("my test:not implemented!") NotImplementedError:my test: not implemented!

从上面的运行结果可以看出,程序识别到了这个方法并没有在子类中实现却被调用了。

从代码报错的行数来看,只有这个子类的实例化对象调用相应的方法的时候才会报错。

这样的推测结论也很容易通过代码修改测试得到验证,此处不再验证。

进一步修改代码:

class ClassDemo: def test_demo(self): raiseNotImplementedError("my test: not implemented!") classChildClass(ClassDemo): def test_demo(self): print("OKOKOOK!") inst =ChildClass() inst.test_demo()

在新的代码中,子类中实现了对test_demo方法的设计。

程序的运行结果如下:

E:\01_workspace\02_programme_language\03_python\OOP\2017\08\10>pythonerror_demo.py OKOKOOK!

从程序的执行结果可以看出,只要相应的方法接口进行了实现,在执行的时候未实施的错误便不会报出。

相关推荐:

详解Python中内置的NotImplemented类型的用法

The above is the detailed content of How to use NotImplementedError in Python programming_python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn