eliben wrote:
Hello,

I have a game class, and the game has a state. Seeing that Python has
no enumeration type, at first I used strings to represent states:
"paused", "running", etc. But such a representation has many
negatives, so I decided to look at the Enum implementation given here:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/413486

So, I've defined:

class Game:
  self.GameState = Enum('running', 'paused', 'gameover')
That can't be what you've got. But I think I can guess what you meant to show here.)
  def __init__
   ... etc

Several options:

Define the Enum outside the class:
   GameState = Enum('running', 'paused', 'gameover')
then later
   class Game:
       ...
       if state == GameState.running:
           ...


Or just simply define some values
 RUNNING = 0
 PAUSED = 1
 GAMEOVER = 2
then later:
   class Game:
       ...
       if state == RUNNING:
           ...


Or try this shortcut (for the exact same effect):
 RUNNING, PAUSED, GAMEOVER = range(3)

Gary Herron






Later, each time I want to assign a variable some state, or check for
the state, I must do:

  if state == self.GameState.running:

This is somewhat long and tiresome to type, outweighing the benefits
of this method over simple strings.

Is there any better way, to allow for faster access to this type, or
do I always have to go all the way ? What do other Python programmers
usually use for such "enumeration-obvious" types like state ?

Thanks in advance
Eli
--
http://mail.python.org/mailman/listinfo/python-list

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to