Python programming method using NotImplementedError

Following small to share for everyone to use a Python programming NotImplementedError has a good reference value, we want to help. Come and see, to follow the small series together
the Python programming can be achieved in raise report an error function, and error conditions can be customized by the programmers themselves go. In object-oriented programming, a method may be reserved first interface is not implemented, achieve its subclasses.

If it requires a subclass must be achieved, not when implemented it will lead to problems, so the way to raise the use of good.

The problem this time is generated classification NotImplementedError.
Write a piece of code as follows:

class ClassDemo:
    def test_demo(self):
           raiseNotImplementedError("my test: not implemented!")
  
classChildClass(ClassDemo):
    pass
  
inst =ChildClass()
inst.test_demo()

Result of the program:

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!

As seen from the results of the above operation, the program identifies this method was not implemented in a subclass called.

From the point of view given the number of lines of code, only instantiate an object of this subclass when calling the appropriate method will be given.

Such a conclusion can be easily estimated by a modified test code is verified, here is no longer verified.

Further modify the code:

class ClassDemo:
    def test_demo(self):
           raiseNotImplementedError("my test: not implemented!")
  
classChildClass(ClassDemo):
    def test_demo(self):
       print("OKOKOOK!")
  
inst =ChildClass()
inst.test_demo()

In the new code, subclass implements test_demo design method.

Operating results of the program are as follows

E:\01_workspace\02_programme_language\03_python\OOP\2017\08\10>pythonerror_demo.py
OKOKOOK!

As seen from the results of execution of a program, as long as the corresponding interface method of implementation, an error in the implementation of the embodiment will not not reported.

We recommend learning Python buckle qun: 913066266, look at how seniors are learning! From basic web development python script to, reptiles, django, data mining, etc. [PDF, actual source code], zero-based projects to combat data are finishing. Given to every little python partner! Every day, Daniel explain the timing Python technology, to share some learning methods and the need to pay attention to small details, click on Join us python gathering place for learners
to use the above Python programming NotImplementedError in this small series is to share the entire contents of everyone

发布了35 篇原创文章 · 获赞 37 · 访问量 3万+

Guess you like

Origin blog.csdn.net/haoxun03/article/details/104255329