Tricks to do "enums"?
dgoodger at bigfoot.com
Sun May 7 22:53:49 EDT 2000
on 2000-05-07 21:27, Courageous (jkraska1 at san.rr.com) wrote:
> So, I've been doing module level enums like this:
> ## My module
> While this works, and gives me great namespace protection
> (individuals using my "enum" prefix it with the module name),
> I'm wondering if there's any trick that will produce these
> without requiring me to renumber them? I'm getting tired
> of typing in new numbers for them if I decide to reorder
> them or insert elements...
Well, if you really meant VAL3=2 (ie, 0, 1, 2, ...; without gaps, a
continuous sequence), then you can do this:
VAL3) = range(3)
This also has the advantage of visually grouping the variables. They don't
have to start at 0 either; see the docs for the range() built-in function.
If your enumeration's values were some other function, you could use map():
TWO3) = map(lambda i: 2**i, range(4))
This would give you the values [1, 2, 4, 8], useful for bitwise operations.
If the variable names were so regular, you could use eval() inside a loop,
constructing Python statement strings. Of course, if the variable names are
that regular, you might as well use an array...
David Goodger dgoodger at bigfoot.com Open-source projects:
- The Go Tools Project: http://gotools.sourceforge.net
(more to come!)
More information about the Python-list