2 回答

TA貢獻(xiàn)1860條經(jīng)驗 獲得超8個贊
您正在將GameStatus初始化程序設(shè)置為None:
class GameStatus(object):
__init__ = None
不要那樣做 Python希望這是一種方法。如果您不想使用__init__方法,則根本不要指定它。最多將其設(shè)為空函數(shù):
class GameStatus(object):
def __init__(self, *args, **kw):
# Guaranteed to do nothing. Whatsoever. Whatever arguments you pass in.
pass
如果要創(chuàng)建類似枚舉的對象,請查看如何在Python中表示“枚舉”?
對于Python 2.7,您可以使用:
def enum(*sequential, **named):
enums = dict(zip(sequential, range(len(sequential))), **named)
reverse = dict((value, key) for key, value in enums.iteritems())
enums['reverse_mapping'] = reverse
return type('Enum', (), enums)
GameStatus = enum('NotStarted', 'InProgress', 'Win', 'Lose')
print GameStatus.NotStarted # 0
print GameStatus.reverse_mapping[0] # NotStarted
添加回答
舉報