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