accessing class attributes

eliben eliben at gmail.com
Wed May 28 12:09:14 EDT 2008


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')

  def __init__
   ... etc

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



More information about the Python-list mailing list