Python Singleton

I always forget it. As a note. (Quoted from expert Python)

my_singleton_class.py


class MySingletonClass(object):

    def __new__(clsObj, *args, **kwargs):
        if not hasattr(clsObj, "__instance__"):
            clsObj.__instance__ = super(MySingletonClass,
                                        clsObj).__new__(clsObj,
                                                        *args,
                                                        **kwargs)
        return clsObj.__instance__

It's a mysterious spell.

Recommended Posts

Python Singleton
Singleton pattern in Python
Python
kafka python
Python Summary
Built-in python
Python comprehension
Python technique
Studying python
Python 2.7 Countdown
Python FlowFishMaster
Python service
python tips
python function ①
Python basics
Python memo
ufo-> python (3)
Python comprehension
install python
Python basics ④
Python Memorandum 2
python memo
Python Jinja2
Python increment
atCoder 173 Python
[Python] function
Python installation
python tips
Installing Python 3.4.3.
Try python
Python memo
Python iterative
Python algorithm
Python2 + word2vec
Implement the Singleton pattern in Python
[Python] Variables
Python functions
Python sys.intern ()
Python tutorial
Python decimals
python underscore
Python summary
Start python
[Python] Sort
Note: Python
Python basics ③
python log
Python basics
[Scraping] Python scraping
Python update (2.6-> 2.7)
python memo
Python memorandum
Python # sort
ufo-> python
Python nslookup
python learning
Hannari Python 2020
[Rpmbuild] Python 3.7.3.
Prorate Python (1)
python memorandum
Download python