Tricks to do "enums"?

Aahz Maruch aahz at netcom.com
Sun May 7 23:58:45 EDT 2000


In article <391618DF.19F55C6C at san.rr.com>,
Courageous  <jkraska1 at san.rr.com> wrote:
>
>So, I've been doing module level enums like this:
>
>##  My module
>
>VAL1=0
>VAL2=1
>VAL3=3
>
>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...

I've seen the other two followups, and I think it's the wrong approach
*unless* you're using variable names to indicate column order in e.g. a
database.  Generally, you're wanting to test a set of enums, so you
should use strings in either a list or a dict:

validEnums = ['val1', 'val2', 'val3']
testEnum = ['val4', 'val1']
for enum in testEnum:
  if enum in validEnums:
    ....

And so on.  There are a variety of different ways of doing this,
depending on the exact result you want.  In general, despite my example
above, I prefer using a dict because it scales better and because you
can attach a value to the enum (key, really) instead of just testing its
existence.
--
                      --- Aahz (Copyright 2000 by aahz at netcom.com)

Androgynous poly kinky vanilla queer het    <*>     http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6

"It's not because my mind is made up that I don't want you to confuse me
with any more facts.  It's because my mind isn't made up.  I already have 
more facts than I can cope with.  So SHUT UP, do you hear me?  SHUT UP!"  
_The Shockwave Rider_, 1975



More information about the Python-list mailing list