Python编程中NotImplementedError的使用方法_python


本文摘自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怎么新建文件夹

ansible作为Python模块库使用的方法

Python中5种连接字符串的方法

Python3下载哪个版本

Python 怎么获取网页内容

Python程序怎么运行结果

Python中len是什么意思

Python函数之divmod数字处理函数

Python多线程爬虫实战_爬取糗事百科段子的实例_Python

Python之前需要学c语言吗

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...