- class Singleton2(type):
- def __init__(cls, name, bases, dict):
- super(Singleton2, cls).__init__(name, bases, dict)
- cls._instance = None
- def __call__(cls, *args, **kw):
- if cls._instance is None:
- cls._instance = super(Singleton2, cls).__call__(*args, **kw)
- return cls._instance
- class MyClass(object):
- __metaclass__ = Singleton2
- one = MyClass()
- two = MyClass()
- two.a = 3
- print one.a
- #3
- print id(one)
- #31495472
- print id(two)
- #31495472
- print one == two
- #True
- print one is two
- #True
时间: 2024-11-11 04:52:48