Elegant Python-__new__, __init__, __call__ silly

Source: Internet
Author: User

__new__: Object creation , is a static method, the first parameter is CLS. (Think about it too, it can't be self, the object hasn't been created, it's self)

__INIT__: initialization of an object, is an instance method, and the first argument is self.

__call__: Object cancall, note is not a class, is an object.


It is created before it is initialized. Namely first __new__, then __init__.

The above is not a good understanding, see examples.

For __new__

Class Bar (object):    passclass foo (object):    def __new__ (CLS, *args, **kwargs):        return Bar () print Foo ()

As you can see, the output is a bar object.

The __new__ method does not have to be written in the class definition and, if not defined, calls object.__new__ to create an object by default . If the definition is override, you can custom Create the object's behavior.

The smart reader might think that since __new__ can create a custom object, I'll do it here, and every time I create an object, it returns to the same one, isn't it just a singleton mode? Yes, that's it. Can observe the elegant Python-singleton mode strum

When you define a singleton pattern, because the custom __new__ overloads the __new__ of the parent class, you explicitly call the parent class's __new__, either object.__new__ (CLS, *args, **kwargs), or super (). , otherwise it is not extend the original instance, but instead of the original instance.


For __call__

Class Foo (object):    def __call__ (self):        PASSF = Foo () #类Foo可callf () #对象f可call


Summary, in Python, the behavior of the class is this way, __new__, __init__, __call__ and other methods are not required to write, will be called by default, if you define, is override, can be custom. Since the override, it is usually explicitly called to compensate for the purpose of extend .

This is also why there is "clearly defined def _init__ (self, *args, **kwargs), how the object does not initialize" this seemingly bizarre behavior. (note, here _init__ less write an underscore, because __init__ not have to write, so here will not error, but as a new method _init__)


Flowing python-__new__, __init__, __call__ silly

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.