What I see more often is this, in top-level module context:
FOO_BAR = 'FOO_BAR'
FOO_BAZ = 'FOO_BAZ'
FOO_QUX = 'FOO_QUX'
...and later...
if something is FOO_BAR: pass # do something here
elif something is FOO_BAZ: pass # do something else
elif something is FOO_QUX: pass # do something else
else: raise Exception('Invalid value for something')
Note that the use of is
rather than ==
is taking a risk here -- it assumes that folks are using your_module.FOO_BAR
rather than the string 'FOO_BAR'
(which will normally be interned such that is
will match, but that certainly can't be counted on), and so may not be appropriate depending on context.
One advantage of doing it this way is that by looking anywhere a reference to that string is being stored, it's immediately obvious where it came from; FOO_BAZ
is much less ambiguous than 2
.
Besides that, the other thing that offends my Pythonic sensibilities re the class you propose is the use of split()
. Why not just pass in a tuple, list or other enumerable to start with?