Python's function module [2], ABC, uses ABC to create an abstract base class

Source: Internet
Author: User

ABC modules /ABC module

In defining an abstract method, Python provides an ABC module to detect whether an abstract method was redefined during the initialization phase.

1  fromAbcImportAbcmeta, Abstractmethod2 3 #Generate a abstract base obj-myabc4 classMYABC (metaclass=Abcmeta):5     #__metaclass__ = Abcmeta6 @abstractmethod7     defRun (self):8         Pass9 Ten classFoo (MYABC): One     defRun (self): A         Print('Run') -  -f =Foo () theF.run ()

Line 4th: Generate an abstract base class MYABC, which takes advantage of the correlation usage of the Metaclass meta class

第6-7: Abstract methods and adorners, abstract methods can not execute any program, but subclasses must redefine this function

Line 10th: Define the subclass of Myabc Foo, at which point if the Run method is not defined in Foo, it will produce an error before running, instead of waiting until after the error

NOTE: ABC. Abcmeta This is the meta-class used to generate the abstract base class. The classes generated by it can be inherited directly.

Related reading

1. Abstract methods
2. Decorative Device

Reference Links

http://blog.csdn.net/igorzhang/article/details/39026885

Python's function module [2], ABC, uses ABC to create an abstract base class

Related Article

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.