equivalent of enum?

Doug Hellmann doughellmann at home.com
Thu Sep 9 20:18:24 EDT 1999


Skip Montanaro wrote:
> 
> If you expect to have properties associated with particular pieces
> (position, strength, etc), you might want to consider representing them by
> instances of a specific ChessPiece class instead:

This is probably the best advice for this situation.

> In more direct answer to your question, there is no enum type in Python.
> It's simple enough to declare objects that you treat as constants, however:

There are a lot of times when having an enumeration is nice, and a class
would be overkill.  I forget where I've seen the idiom, and I certainly
didn't make it up, but a really nice way to make an enum is to do
something like:

	one, two, three = range(3)

	print one, two, three

Works every time.  If you forget to increase the range size when you add
a new value, you will get a runtime error because the tuple unpack won't work.

Doug




More information about the Python-list mailing list