Python Learning Notes

Source: Internet
Author: User

1.loop

2. Single-case mode

3.__new__ and Singleton modes

in the actual application process, we can use this:

1 classlxmldocument (object_ref):2Cache =Weakref. Weakkeydictionary ()3   __slots__= ['__weakref__']4  5   def __new__(CLS, Response, parser=etree. Htmlparser):6Cache =Cls.cache.setdefault (response, {})7     ifParser not inchCache:8obj = Object_ref.__new__(CLS)9Cache[parser] =_factory (response, parser)Ten     returnCache[parser] One       

The use of the __new__ () method in this class is to check for the presence of the object in the cache before initializing it, to return the cached object directly if it exists, and to put the object in the cache for the next use if it does not exist.

In a single example, a single example can be implemented by overloading __new__:

1 class Singleton (object): 2     def __new__ (CLS, *args, * *Kwargs): 3         if  not ' _instance ' ):4             cls._instance = Super (Singleton, CLS).  __new__(CLS, *args, * *Kwargs)5         return Cls._instanc
Comparison of three special methods of __new__, __init__ and __call__

__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)

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.
When you define a singleton pattern, because the custom __new__ overloads the parent class's __new__, 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

__INIT__: Initialization of an object, is an instance method, and the first argument is self.
__call__: Object can call, note is not a class, is an object.

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

Python Learning Notes

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.