Saturday, 7 February 2009

Enumerated Types in Python

How do we do enumerated types work in Python? Well, there is no enum keyword as such but there are many ways to get the same effect.

red,green,blue=range(3)

You can also isolate your enums in a class. e.g.

class Colors:
red,green,blue=range(3)

Colors.red yields 0 and dir(Colors) yields meaningful results.
Useful for programming a finite state machine.

No comments: