new enum idiom

Carel Fellinger cfelling at iae.nl
Mon Jan 8 17:01:28 EST 2001


Alex Martelli <aleaxit at yahoo.com> wrote:
...
> This assumes a non-string argument (typically an integer, but that's
> not checked here) is meant to set the value to be named by the
> string-argument immediately *following* it -- a substantial
> simplification.  So substantial, that if one
...
> Needing to explicitly set values in an enum is rare enough, that it
> would seem fully acceptable to me to place the explicit values right
> _before_ their names in those rare cases, using the much-simpler
> code above.  Viva simplicity...!-)

Simplicity of implementation, yes.  But is this what one would expect?
I think all the languages I know that provide an enumaration method
that allows for changing the count midway spell it the other way
around, first the name then the value.  So I strongly argue that in
this particular case you better invest a little more in the coding of
this enum class to prevent all those easy slip of the keys in the use
of it, especially since its use is so rare.  Arguely an even better
approach would be to have such new counting points be spelled as
tuples; a little more eye-catching and simplifying the code even
further:)


   def enum(*args):
       class Enum: pass
       curval = 0
       for arg in args:
   	   if type(arg)==type(()):
   	       arg, curval = arg
   	   setattr(Enum, arg, curval)
   	   curval += 1
       return Enum
   
   
   MidiEvent = enum("NOTE_ON", ("NOTE_OFF", 2), "POLYPHONIC_KEY_PRESSURE"
        # dozens and dozens more...
        )
 

-- 
groetjes, carel



More information about the Python-list mailing list