Python learns _13_ inheritance and meta classes

Source: Internet
Author: User

Inherited

The implication of inheritance is that subclasses inherit the namespace of the parent class, which can invoke the properties and methods of the parent class, and because of the way the namespace is found, when the child class defines the property or method with the same name as the parent class, the instance of the subclass calls the attribute in the subclass, not the parent class, which forms the polymorphism in Python:

Def superclass:
? ? def A_method:
? ? ? ? Pass
def subclass (Superclass):
? ? def A_method:
? ? ? ? Pass
obj = Subclass ()
Obj.a_method ()

When obj calls a method, it finds the local namespace of the A object itself, then finds it in subclass, and then finds it in superclass, and any finding of that property terminates the lookup process, so the A_ in the example above is called by obj Method belongs to subclass. Seeing the objects in Python again is a relationship of namespaces.

Python supports multiple inheritance at the same time, a class can inherit from more than one parent class, and in a multiple-inheritance relationship, the property is looked up in a breadth-first search (beginning with depth first), which is searched sequentially from each parent class, and, if not found, in order by all the parent classes of the first parent class. Then all the parent classes of the second parent class, followed by all the parent classes of the first parent of the first parent class ... The advantage of finding this way is that you always inherit attributes from the first class, and you must be aware of the order when you actually look for them.

Meta class

If the class is the template that creates the instance, then the meta-class is the template that creates the class, distinguishing between the meta-class and the parent class, and the meta-class simply defines the constructor of the class: The __new__ method, which is called from the default meta-class type if no meta-class is specified:

Class Listmetaclass (Type):
? ? Def __new__ (CLS, name, bases, attrs):?
? ? ? ? attrs[' Add ' = Lambda self, value:self.append (value)
? ? ? ? Return type.__new__ (CLS, name, bases, Attrs)
Class MyList (List,metaclass=listmetaclass):
? ? Pass
Print (listmetaclass.__class__)

Unlike classes that use factory functions to instantiate objects, the Meta class is indicated by using Metaclass in the class definition. The Add property is added to MyList through the __new__ constructor method, so there is no add property in Listmetaclass. The essence of a meta-class is a class, but a class that defines a special __new__ method.

Python learns _13_ inheritance and meta classes

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.