[Python-ideas] Enums

Bruce Leban bruce at leapyear.org
Thu Jul 28 03:14:12 CEST 2011


On Wed, Jul 27, 2011 at 5:30 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:

>
> This recipe is pretty much the full extent of the proposal (perhaps
> with the automatic type generation I mention in the discussion
> section):
> http://code.activestate.com/recipes/577810-named-values/
>
>

I like the NamedValue/namedvalue recipe. I prefer module constants like
os.SEEK_END to nested names like os.Seek.End and this encourages that. If
someone decides to change os module to:

    SEEK_END = namedvalue('SEEK_END', 2)

that won't break anything. For the simple enum case, a decorator could
transform

    @enum
    class Color:
        red = 1
        green = 2

into:

    class Color:
        red = namedvalue('Color.red', 1)
        green = namedvalue('Color.green', 2)

--- Bruce
Follow me: http://www.twitter.com/Vroo http://www.vroospeak.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110727/da1f6fdd/attachment.html>


More information about the Python-ideas mailing list