Understanding of Six.with_metaclass (Abcmeta, Object)

Source: Internet
Author: User

In the course of learning python, you see how to generate virtual base classes,
class peoplebase (Six.with_metaclass (Abcmeta, Object)):    @abstractmethod    def work (self, *args, * *Kwargs):        #Pass is an empty statement, placeholder        pass    @abstractmethod     def Live (self, *args, * *Kwargs)        :Pass
For Six.with_metaclass (Abcmeta, object) do not understand, find some information, one of the blog on the meta-class explanation of the more detailed, the link is as follows: the meta-class in Python (translated): https://www.cnblogs.com/ ajianbeyourself/p/4052084.html above the blog is longer, only about Six.with_metaclass (Abcmeta, object) related content, do a little note class is the class to create classes, in Python, Only the type class and its subclasses can be used when the meta-class, type creates the class, parameter format: type (classname, parentclasses, attrs), ClassName is the class name, string type, parentclasses is all the parent class of the class, A tuple type, attrs is a class of all {properties: Values}, is a dictionary type all classes are created eventually by calling Type.__new__ (CLS, classname, Bases, Attrs), which creates a class object in the heap and returns the object when passed type.__ new__ (CLS, classname, bases, attrs) when creating a class, the CLS is the class's meta-class, which is the type class or its subclasses. Abcmeta is an abstract class of meta-classes used to create abstract class base classes: Metaclass for defining abstract base Classes (ABCs). Six.with_metaclass is a method that uses a meta class to create a class, calling an inner class , in the __new__ function of the inner class, returns a newly created temporary class
defWith_metaclass (Meta, *bases):"""Create a base class with a metaclass."""    #This requires a bit of explanation:the basic idea was to make a dummy    #Metaclass for one level of class instantiation This replaces itself with    #The actual metaclass.    classmetaclass (type):def __new__(CLS, name, this_bases, D):returnMeta (name, bases, D) @classmethoddef __prepare__(CLS, Name, this_bases):returnMeta.__prepare__(name, bases)returnType.__new__(Metaclass,'Temporary_class', (), {})
Six.with_metaclass (Abcmeta, object) returns a temporary class created by a Abcmeta meta class through the __new__ function of the inner class .as the base class for the Peoplebase classin Python, the class is also an object, the following two statements can see the type of Peoplebase class, are <class ' abc. Abcmeta ', that is, the Peoplebase class is an object of the Abcmeta meta class, generated by the Abcmeta Meta class.
Print (Type (peoplebase)) Print (Peoplebase. __class__ )#<class ' abc. Abcmeta ' >#<class ' abc. Abcmeta ' >

Understanding of Six.with_metaclass (Abcmeta, Object)

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.