How to use Notimplementederror in Python programming _python

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.