
# 无实例
class NoInstance(type):
    def __call__(self, *args, **kwargs):
        raise TypeError('Can not instantiate directly')


class Spam(metaclass=NoInstance):
    @staticmethod
    def grok(x):
        print('Spam.grok', x)


# 单例
class Singleton(type):
    def __init__(self, *args, **kwargs):
        self.__instance = None
        super().__init__(*args, **kwargs)

    def __call__(self, *args, **kwargs):
        if self.__instance is None:
            self.__instance = super().__call__(*args, **kwargs)
            return self.__instance
        else:
            return self.__instance


class SingleSpam(metaclass=Singleton):
    def __init__(self):
        print('Creating Spam (singleton)')


# -------------
class Test(object):

    # 无实例
    @staticmethod
    def noinstance():
        Spam.grok(41)
        s = Spam()

    # 单例
    @staticmethod
    def singleton():
        a = SingleSpam()
        b = SingleSpam()
        if a == b:
            print('T')
        else:
            print('F')



if __name__ == '__main__':

    # Test.noinstance()

    Test.singleton()








@@ -1,8 +1,24 @@
+import weakref%0A%0A
 %0A# %E6%97%A0%E5%AE%9E%E4%BE%8B%0Ac
@@ -724,309 +724,1159 @@
 %0A%0A# 
--------------%0Aclass Test(object):%0A%0A    # %E6%97%A0%E5%AE%9E%E4%BE%8B%0A    @staticmethod%0A    def noinstance():%0A        Spam.grok(41)%0A        s = Spam()%0A%0A    # %E5%8D%95%E4%BE%8B%0A    @staticmethod%0A    def singleton():%0A        a = SingleSpam()%0A        b = SingleSpam()%0A        if a == b:%0A            print('T')%0A        else:%0A            print('F
+%E7%BC%93%E5%AD%98%E5%AE%9E%E4%BE%8B%0Aclass Cached(type):%0A    def __init__(self, *args, **kwargs):%0A        super().__init__(*args, **kwargs)%0A        self.__cache = weakref.WeakValueDictionary()%0A%0A    def __call__(self, *args):%0A        if args in self.__cache:%0A            return self.__cache%5Bargs%5D%0A        else:%0A            obj = super().__call__(*args)%0A            self.__cache%5Bargs%5D = obj%0A            return obj%0A%0A%0Aclass CacheSpam(metaclass=Cached):%0A    def __init__(self, name):%0A        print('Creating Spam(%7B!r%7D)'.format(name))%0A        self.name = name%0A%0A# -------------%0Aclass Test(object):%0A%0A    # %E6%97%A0%E5%AE%9E%E4%BE%8B%0A    @staticmethod%0A    def noinstance():%0A        Spam.grok(41)%0A        s = Spam()%0A%0A    # %E5%8D%95%E4%BE%8B%0A    @staticmethod%0A    def singleton():%0A        a = SingleSpam()%0A        b = SingleSpam()%0A        if a == b:%0A            print('T')%0A        else:%0A            print('F')%0A%0A    # %E7%BC%93%E5%AD%98%0A    @staticmethod%0A    def cache():%0A        a = CacheSpam('alpha')%0A        b = CacheSpam('beta')%0A        c = CacheSpam('alpha')%0A%0A        if a is b:%0A            print('a is b')%0A        else:%0A            print('a is not b')%0A%0A        if a is c:%0A            print('a is c')%0A        else:%0A            print('a is not c
 ')%0A%0A
@@ -1933,16 +1933,18 @@
 e()%0A%0A   
+ #
  Test.si
@@ -1954,14 +1954,31 @@
 eton()%0A%0A
+    Test.cache()%0A
 %0A%0A%0A%0A%0A%0A

add cache instance