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将dataframe的某一列作为index的方法

Python类的继承是什么?类的继承有什么样的规则?

Python中关于文件名与文件路径操作的实例

Python queue模块

cookie介绍和模拟登录演示

怎么查看Python的安装路径?

Python求n的阶乘

人生苦短我用Python是什么梗

没学过编程可以学Python

Python怎么调试程序

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




打赏

取消

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

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

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

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

评论

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