-1

Hi next thing is bothering me:

I'm trying to use the next class:

class GameStatus(object):
"""Enum of possible Game statuses."""
__init__ = None
NotStarted, InProgress, Win, Lose = range(4)
def getStatus(number):
    return{
        0: "NotStarted",
        1: "InProgress",
        2: "Win",
        3: "Lose",
        }

in another class(both in same py file). In this another class in his method init i do next thing:

class Game(object):
"""Handles a game of minesweeper by supplying UI to Board object."""
gameBoard = []
gs = ''
def __init__(self, board):
    self.gameBoard = board
    gs = GameStatus() //THIS IS THE LINE

And when i try to run the game i get next error message:

File "C:\Users\Dron6\Desktop\Study\Python\ex6\wp-proj06.py", line 423, in __init__
gs = GameStatus()
TypeError: 'NoneType' object is not callable

What am i doing wrong?

4

2 回答 2

1

您将GameStatus初始化程序设置为None

class GameStatus(object):
    __init__ = None

不要那样做。Python 期望这是一个方法。如果你不想有一个__init__方法,就不要指定它。最多将其设为空函数:

class GameStatus(object):
    def __init__(self, *args, **kw):
        # Guaranteed to do nothing. Whatsoever. Whatever arguments you pass in.
        pass

如果您想创建一个类似枚举的对象,请查看如何在 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
于 2013-06-09T10:45:15.507 回答
0

好的,经过小型研究,我发现了问题。我得到的代码是:

class GameStatus(object):
    """Enum of possible Game statuses."""
    __init__ = None
    NotStarted, InProgress, Win, Lose = range(4)

我需要将 nymbers 转换为他们的价值。所以我建立:

def getStatus(number):
return{
    0: "NotStarted",
    1: "InProgress",
    2: "Win",
    3: "Lose",
    }

并且无法使用它,因为我无法创建对象,而且这种方法不是静态的。解决方案:在方法前添加@staticmethod。

另外,我的返回开关有一个小错误,正确的版本是:

@staticmethod
def getStatus(number):
return{
    0: "NotStarted",
    1: "InProgress",
    2: "Win",
    3: "Lose",
    }[number]

感谢所有试图提供帮助的人。

于 2013-06-10T13:41:44.940 回答