Understanding Python Super () class

Source: Internet
Author: User

First of all, we have a question about the Python super class in StackOverflow as the beginning of this article, the problem is basically this:

>>>classA (object): ...def __init__(self): ...Print "A Init"... super (a,self) .__init__()... >>> >>> A =A () a init>>>classB (object): ...def __new__(self): ...Print "B New"... super (b,self) .__new__()... >>> >>> B =B () b Newtraceback (most recent call last): File"<stdin>", Line 1,inch<module>File"<stdin>", Line 4,inch __new__Typeerror:object.__new__(): notenough arguments>>>

You can see that if you invoke the __init__ () method of the Super class and call the __new__ () class, you will get an error, and the error says that the parameter is insufficient. The following code is correct for using the Super class's __new__ () method.

class B (object): ...      def __new__ (self): ...              Print " B New " ...             Super (b,self). __new__  >>> >>> b = B () b New

Why do I need to display the delivery classes and class instances in the Super Class __new__ (self)?

Because __new__ is a static method, the static method is detached from a method other than the class itself, so he does not bind the class (class instance) itself as an implicit argument, so super (...). __new__ returns a static method as well. So the delivery needs to be shown. Detailed reading http://www.python.org/download/releases/2.2/descrintro/#__new__

What is super ()?

What is super ()? Super is a built-in type. Using super in an object often gives the illusion that the parent class object is returned. Actually, he is returning an object of the Super class. When instantiated, the first parameter is a type, the second argument can be a instance instance of type, or it can be a subclass of type. You call super (Base, self) to return a __class__ = Super object, which can be used as a proxy to access the first attribute in the base class that directly defines the target attribute, in the order of BFS. In fact, it is not necessary to use self as the second parameter of super, and even super does not have to be called within class:

class A (object): ...      def foo (self): ...              Print  class  B (A     ): ... def __init__ (self, name): ...              =>>> b = B ('b')>>> super (b, b). foo () b 

A discussion of static methods you can view one of my previous articles:

Http://www.cnblogs.com/kennyhr/p/3935465.html

This article references:

http://segmentfault.com/q/1010000000267263

Understanding Python Super () class

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.