_ New _ and _ init _

Source: Internet
Author: User

_ New _ is a rarely used function in the python object-oriented language. More uses the _ init _ function. For example:

class Book(object):    def __init__(self, title):        super(Book, self).__init__(self)        self.title = title# Define a bookb = Book(‘The Django Book‘)print b.title

 

 

The above is the entry code of the OOP language. For a rough look, _ init _ is the same as the constructor in Java. In fact, it is not actually a constructor that cannot be computed at all. _ New _ is the method for creating an instance.

According to official documents:

  • _ Init _ is called after the instance object is created, and then some initial values of the object property are set.

  • _ New _ is called before the instance is created, because its task is to create an instance and then return the instance, which is a static method.

That is, __new _ is called before _ init _, And the return value (Instance) of __new _ is passed to the first parameter of the _ init _ method, then _ init _ sets some parameters for the instance.

class Book(object):    def __new__(cls, title):        print ‘__new__‘        return super(Book, cls).__new__(cls)            def __init__(self, title):        print ‘__init__‘        super(Book, self).__init__(self)        self.title = title        b = Book(‘The Django Book‘)print b.title

 

 

The preceding execution result:

__new____init__The Django Book

 

 
Use Cases of _ new _

The official document specifies the two usage methods of the _ new _ method.

Allowed to inherit immutable types (STR, Int, tuple)

There are also many examples of this. The examples found on the Internet are basically theoretical and are rarely used in practice.

Use metaclass

Metaclass is the syntactic sugar of Python. In short, it can dynamically generate or change the class definition.

A more practical example is how to access the current request during Django admin form verification. The stackflow link is as follows:

Http://stackoverflow.com/questions/1057252/how-do-i-access-the-request-object-or-any-other-variable-in-a-forms-clean-met/6062628#6062628

The first thing that comes to mind is to pass the request over and use it in the clean method.

Class myform (forms. modelform): def _ init _ (self, * ARGs, ** kwargs): Self. request = kwargs. pop ('request') Super (myform, self ). _ init _ (* ARGs, ** kwargs) def clean (Self): # Here we can get self. request Information pass

 

 

Use the following code to call a common view:

f = MyForm(request.POST, request=request)

 

 

However, it does not work when modeladmin is customized, because admin only provides the get_form method, and the returned value is a class object instead of an instance object.

Get_form (self, request, * ARGs, ** kwargs): # This line of code is incorrect # Return myform (request = request) return myform # OK

 

 

The _ new _ method can be used to solve this problem.

def get_form(self, request, *args, **kwargs):    class ModelFormMetaClass(MyForm):        def __new__(cls, *args, **kwargs):            kwargs[‘request‘] = request            return MyForm(*args, **kwargs)    return ModelFormMetaClass

 

 

The calling code of add_view is as follows:

Def add_view (self, request, form_url = '', extra_context = none )"... modelform = self. get_form (request) If request. method = 'post': form = modelform (request. post, request. files) # obtain the request parameter # print form. request if form. is_valid (): Pass else :... (calculate initial) form = modelform (initial = initial)

 

 

Analysis: form = modelforpolicaclass (request. post, request. files), according to the general understanding, an instance of modelformmetaclass should be returned on the right. Because the _ new _ function is rewritten, the parent class function is not called, instead, a myform instance with the request parameter is directly returned, and then the _ init _ function is called. Therefore, modelforpolicaclass () returns this instance, what is needed on the left is the Instance Object of myform. Therefore, the _ new _ function is used to create an instance.

Note: metaclass reduces the readability of the Code and has an alternative solution. It is not recommended for projects. If you are interested, refer to here.

_ New _ and _ init _

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.