本文摘自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
Python字典的清单如何使用
9种Python web程序的部署方式小结
Python 拷贝特定后缀名文件,并保留原始目录结构的实例
调试设置中的Python路径无效怎么办
构造函数的使用方法介绍
Python前景怎么样
探索Python模块自动搜索路径
介绍Python应用学习之qrcode生成二维码
Python fromkeys函数是什么?fromkeys函数的作用是什么?
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » Python编程中NotImplementedError的使用方法_python