# 单例模式与抛出异常
class GamePlayer(object):
instance = None
def __new__(cls, *args, **kwargs):
if cls.instance is None:
cls.instance = super().__new__(cls)
return cls.instance
init_flag = False
def __init__(self):
if GamePlayer.init_flag:
return
print("对象初始化中...")
GamePlayer.init_flag = True
try:
player1 = GamePlayer()
print(player1)
player2 = GamePlayer()
print(player2)
except Exception as result:
print("%s 未知错误" % result)
|
|