本文摘自php中文网,作者不言,侵删。
下面为大家分享一篇Python编程中NotImplementedError的使用方法,具有很好的参考价值,希望对大家有所帮助。一起过来看看吧Python编程中raise可以实现报出错误的功能,而报错的条件可以由程序员自己去定制。在面向对象编程中,可以先预留一个方法接口不实现,在其子类中实现。
如果要求其子类一定要实现,不实现的时候会导致问题,那么采用raise的方式就很好。
而此时产生的问题分类是NotImplementedError。
写一段代码如下:
1 2 3 4 5 6 7 8 9 | class ClassDemo:
def test_demo(self):
raiseNotImplementedError("my test: not implemented!")
classChildClass(ClassDemo):
pass
inst =ChildClass()
inst.test_demo()
|
程序运行结果:
1 2 3 4 5 6 7 | 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<module>
inst.test_demo()
File "error_demo.py", line 3, intest_demo
raise NotImplementedError("my test:not implemented!")
NotImplementedError:my test: not implemented!
|
阅读剩余部分
相关阅读 >>
Python中如何将列表中的字符串连接成一个长路径的实例
Python中replace的用法是什么?
Python可以做游戏辅助吗
Python常见语句汇总
Python中seth是什么
ipad上可以运行Python么
Python中subprocess类与常量的详细介绍
Python怎么卸载模块
Python动态定义函数的方法介绍
Python环境下如何配置pydev插件
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » Python编程中NotImplementedError的使用方法_python