LoginSignup
13
13

More than 5 years have passed since last update.

Python の Singleton

Posted at

いつも忘れるので。メモとして。(エキスパート 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__

不思議な呪文だ。

13
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
13