The following for you to share a Python programming in the use of notimplementederror, with a good reference value, I hope to be helpful to everyone. Come and see it together.
In Python programming, raise can deliver errors, and the conditions for error can be customized by the programmer himself. In object-oriented programming, it is possible to reserve a method interface that is not implemented in its subclasses.
If it is required that its subclasses must be implemented, when not implemented, it can cause problems, then the way to adopt raise is very good.
The problem that arises at this time is the classification of notimplementederror.
Write a section of code as follows:
Class Classdemo: def test_demo (self): raisenotimplementederror ("My Test:not implemented!") Classchildclass ( Classdemo): Pass Inst =childclass () Inst.test_demo ()
Program Run Result:
E:\01_workspace\02_programme_language\03_python\oop\2017\08\10>pythonerror_demo.pytraceback (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 can be seen from the running results above, the program recognizes that this method is not implemented in subclasses but is called.
Judging from the number of lines of code error, only the instantiated object of this subclass calls the corresponding method.
Such a speculative conclusion is also easily verified by code modification tests, which are no longer validated here.
To 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, the subclass implements the design of the Test_demo method.
The running results of the program are as follows:
e:\01_workspace\02_programme_language\03_python\oop\2017\08\10>pythonerror_demo.pyokokook!
From the implementation of the program can be seen, as long as the corresponding method interface has been implemented, in the execution of the implementation of the error will not be reported.