Python single case

Source: Internet
Author: User

Python implementation of a single-instance implementation method.

The singleton mode can ensure that there is only one instance of a class in the system, and the instance is easy to be accessed by the outside world, thus it is convenient to control the number of instances and save system resources. Singleton mode is the best solution if you want to have only one object for a class in the system.

#Coding:utf-8" "implementation of a single example implementation of Adorners" "defSingleton (CLS, *args, * *Kwargs): Instance= {}    def_singleton ():ifCls not inchInstance:instance[cls]= CLS (*args, * *Kwargs)returninstancereturn_singleton@singletondefMyClass (): a= 1def __init__(Self, x=0): self.x=xa=MyClass () b=MyClass () a.a= 3PrintB.A#3A isb#True

The so-called Singleton is a class that can only create one instantiation.

    • A class can have only one instance
    • It must create this instance on its own
    • This instance must be provided to the entire system on its own

Python single case

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.