[Python-ideas] PEP for enum library type?

Jan Kaliszewski zuo at chopin.edu.pl
Thu Feb 14 01:57:50 CET 2013


13.02.2013 00:57, Georg Brandl wrote:

> We could even allow
>
> class Color(Enum):
>     RED = 1
>     GREEN = ...       # becomes 2
>     BLUE = ...        # 3
>     MAGENTA = 5
>     FLUFL = ...       # 6
>
> class Color(FlagEnum):
>     RED = 1
>     GREEN = ...       # becomes 2
>     BLUE = ...        # 4
>     MAGENTA = 16
>     FLUFL = ...
>
> class Color(StringEnum):
>     RED = ...         # becomes 'red'
>     GREEN = ...       # etc.
>     BLUE = ...

It's nice. But what about synonymous items?

     class Color(Enum):
         RED = R = 1
         GREEN = ...
         BLUE = B = ...    # we ment BLUE = B = 3, but effectively
                           # we'll have BLUE = 3 and B = 4  :-|

What about:

(all comments are for explanation only)

     class Color(Enum):
         RED = 1              #  1
         GREEN = +one         #  2
         BLUE = +one          #  3
         YELLOW = Y = +one    #  4
         BLACK = B = 10       # 10
         ORANGE = -one        #  9
         VIOLET = -one        #  8

     class Flag(Enum):
         FOO = 1              #   1
         BAR = +rot           #   2
         BAZ = +rot           #   4
         BOO = +rot           #   8
         SPAM = 256           # 256
         HAM = -rot           # 128
         RAM = -rot           #  64

     class Color(Enum):
         RED = ...            # 'RED'
         GREEN = ...          # 'GREEN'
         BLUE = ...           # 'BLUE'

and maybe also:

     class Color(Enum):
         # 0     1     2     3
         RED, GREEN, BLUE, YELLOW, *end = seq()

     class Color(Enum):
         # 3     4     5     6
         RED, GREEN, BLUE, YELLOW, *end = seq(3)

     class Flag(Enum):
         # 1    2    4    8    16
         FOO, BAR, BAZ, BOO, SPAM, *end = flags()

(yes, it *is* possible to implement it without playing with stack 
frames...)


Cheers.
*j


PS. And now for something completely different: :-)

     @enum
     def Color(v):
         v.RED       # 0
         v.GREEN     # 1
         v.BLUE      # 2
         v.YELLOW    # 3

     @enum
     def Color(v):
         v.RED = 7
         v.GREEN     # 8
         v.BLUE      # 9
         v.YELLOW    # 10




More information about the Python-ideas mailing list