Use of Super in Python (ii)

Source: Internet
Author: User

1. How to use Super

When I started to use super with the example code, I thought it was simple, just like the super in the language of other single-inheritance mechanisms. However, when you try to mix things like bound Super object, with Unbound super Object,metaclass,proxy Object,descriptor,mro,multiple inheritance This concept together to understand the time, the brain will become larger, feel the mechanism behind the thing is so complex, 1:30 it will be really difficult to figure out.

After reading some of the other people's writing, especially after watching a Michele Simionato (estimated to be a man in the Python field) three articles, the super has a little understanding. Attached, links to three articles.

Things to Know about Python Super [1 of 3]

Things to Know about Python Super [2 of 3]

Things to Know about Python Super [3 of 3]

Yes, the author has been spitting from beginning to end, spitting out different versions of the Super bug, pointing out some of the cases where design use cases are wrong. In fact, it is also a good way to learn, learn the mistakes others find, and try to avoid using them.

At the interactive terminal, enter Help (super) to get some part of the information about super.

Class Super (object)
| Super () Same as Super (__class__, <first argument>)
| Super (Type)-Unbound Super Object
| Super (type, obj), bound Super object; Requires Isinstance (obj, type)
| Super (Type, type2), bound Super object; Requires Issubclass (type2, type).

Four use, personal feeling, the first use of the most convenient and safe, the compiler will automatically add two parameters according to different circumstances, in a completely alternative to the third and 42, the second method, the document does not describe the use case, some people say that it is used in other classes to do attributes, for the time being not very understanding.

Requires Isinstance (obj, type), obj, is not necessarily a direct instance of it, an instance of a subclass can also, only so that obj is unchanged, the type parameter has changed (along the type (obj)

. MRO order), it is possible to call methods in all ancestor classes in the order of MRO.

Issubclass (type2, type). Type2 can be the same as type. This method is mainly used in the invocation of the ancestor class Classmethod.

The Super object acts as a "proxy" object, its initialization information, which inevitably needs to pass in information related to the object to be proxied (type,obj), because Super wants to calculate the next ancestor class in the MRO, and type to calculate the self parameter of mro,obj as the Ancestor class method.

As for, binding and unbound super objects can be understood by borrowing the above-transformed objects in Java.

Non-binding, the resulting super object, the effect is equivalent to the ancestor class, the binding super object, can be regarded as the object of the derived class into the ancestor class of the object, super (). property name, or Super (). property name (), not necessarily called method of the computed ancestor class, It is still searching along the MRO, invoking the first method or property that was searched. As for how to return a bound method object, it is possible to generate a bound method object directly by Methodtype using obj and the method object of the searched ancestor class, (

pyobject* pymethod_new (pyobject *func, pyobject *self )
Return value:new Reference.

Return a new Method object, with func being any callable object and The instance the method should Be bound. func is the function, that would be, called when the method is called. self must not being NULL.

According to this document, I think this speculation should be reasonable.

classA ():defFun (self):Print("AAAAA")classB (A):PassclassC (B):Passcc=C ()Print("Print cc", cc) fun=Super (C, CC). FunFun ()Print("print the object bounded to fun method\n", fun.__self__)

Operation Result:

Print cc <__main__. C object at 0x00c0f5b0>AAAAAprint<__main__. C Object at 0x00c0f5b0>

FAQ for 2.super

1. Try not to use it when calling, super (self, self), which will fall into infinite recursion.

2. Parameter transfer problem, when overriding the method, try to use *args and **kwargs

3. Some features of the ancestor class, such as "" operations, such as rewriting the __getattr__, but the Super object can not be used, after all, just a proxy object, can not fully implement the full functionality of the Ancestor object.

Use of Super in Python (ii)

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.