-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?

Andrey Dobrikov
  • 445
  • 2
  • 18

2 Answers2

1

You are setting the GameStatus initializer to None:

class GameStatus(object):
    __init__ = None

Don't do that. Python expects that to be a method. If you do not want to have a __init__ method, do not specify it at all. At most, make it an empty function:

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

If you wanted to create an enum-like object, take a look at How can I represent an 'Enum' in Python?

For Python 2.7, you could use:

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
Community
  • 1
  • 1
Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997
0

Ok so after small research i found the problem. The code i got was:

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

I needed to convert nymbers to their value. So i build:

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

And couldnt use it, because i couldn't create an object, and this mothod wasn't static. The solution: Add @staticmethod before the method.

Plus i had one small error with the return switch, the correct version which works is:

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

Thanks for all who tried to help.

Andrey Dobrikov
  • 445
  • 2
  • 18