python中raise NotImplementedError的使用

raise NotImplementedError的使用感觉很类似于C#中虚函数的效果,它的意思是如果这个方法没有被子类重写,但是调用了,就会报错。
错误的使用:
代码

# coding=utf-8
class testClassOne(object):
    def Test(self):
        raise NotImplementedError

class testClassTwo(object):
    def Test2(self):
        print "hello world"

a = testClassTwo()
a.Test()

执行效果
在这里插入图片描述

正确的使用

# coding=utf-8
class testClassOne(object):
    def Test(self):
        raise NotImplementedError

class testClassTwo(object):
    def Test(self):
        print "hello world"

a = testClassTwo()
a.Test()

执行效果
在这里插入图片描述

发布了202 篇原创文章 · 获赞 210 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_40666620/article/details/105026716