本文摘自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如何实现堆栈与队列的实例详解
Python如何实现注册登录系统的实例详解
Python 列表解析
Python处理菜单消息操作示例
Python的单线程多任务的实现
Python中的模块string.py
Python中json模块和pickle模块的简单介绍(附示例)
Python字典的操作总结(附示例)
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » Python编程中NotImplementedError的使用方法_python