equivalent of enum?

Remco Gerlich scarblac-spamtrap at pino.selwerd.cx
Thu Sep 9 18:17:44 EDT 1999


Is there an equivalent for enumerated types in Python?

I want to make some chess-related routines; first, implement
something that can hold a chess position, game score, etc.

The board has squares, that can hold a piece. I want those
to be represented by integers (whiterook is 1, whiteknight is 2,
etc). But I want to refer to them by name in the code, not
use the magic number 2 everywhere, and I want to index
arrays with them.

Using a hash (to do piece["whiterook"]) isn't really an option,
it doesn't need to be fast now, but later I want to implement
crucial parts in as-fast-as-possible C, since positions and
moves are in the centre of any loops in chess software; it
would be too slow.

The thing I've come up with so far is using variables,
so I have a file import.py that does

whiterook=1
whiteknight=2

etc, that I can import. I had expected there to be some nicer
way to do this, though.

-- 
Remco Gerlich,  scarblac at pino.selwerd.cx
 12:14am  up 23 days,  6:40,  6 users,  load average: 0.16, 0.06, 0.03




More information about the Python-list mailing list