[阅读: 336] 2008-06-23 08:21:03
class Singleton(type):
''' Singleton metaclass. Use by defining the metaclass of a class Singleton,
e.g.: class ThereCanBeOnlyOne:
__metaclass__ = Singleton
'''
def __call__(class_, *args, **kwargs):
if not class_.hasInstance():
class_.instance = super(Singleton, class_).__call__(*args, **kwargs)
return class_.instance
def deleteInstance(class_):
''' Delete the (only) instance. This method is mainly for unittests so
they can start with a clean slate. '''
if class_.hasInstance():
del class_.instance
def hasInstance(class_):
''' Has the (only) instance been created already? '''
return hasattr(class_, 'instance')

机器人,
这首歌学会了没有?

我们的目标是->没有蛀牙!
