Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #106932 > unrolled thread

Enum questions.

Started byAntoon Pardon <antoon.pardon@rece.vub.ac.be>
First post2016-04-13 12:12 +0200
Last post2016-04-13 13:58 +0300
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Enum questions. Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-04-13 12:12 +0200
    Re: Enum questions. Rustom Mody <rustompmody@gmail.com> - 2016-04-13 03:34 -0700
      Re: Enum questions. Marko Rauhamaa <marko@pacujo.net> - 2016-04-13 13:58 +0300

#106932 — Enum questions.

FromAntoon Pardon <antoon.pardon@rece.vub.ac.be>
Date2016-04-13 12:12 +0200
SubjectEnum questions.
Message-ID<mailman.67.1460542399.15650.python-list@python.org>
I have been looking at the enum documentation and it
seems enums are missing two features I rather find
important.

1) Given an Enum value, someway to get the next/previous
   one

2) Given two Enum values, iterate over the values between
   them.

Did I miss those in the documentation or are they really
missing?

-- 
Antoon.

[toc] | [next] | [standalone]


#106933

FromRustom Mody <rustompmody@gmail.com>
Date2016-04-13 03:34 -0700
Message-ID<8e3c705a-6d73-49fa-abcf-8b1197a83e5e@googlegroups.com>
In reply to#106932
On Wednesday, April 13, 2016 at 3:43:41 PM UTC+5:30, Antoon Pardon wrote:
> I have been looking at the enum documentation and it
> seems enums are missing two features I rather find
> important.
> 
> 1) Given an Enum value, someway to get the next/previous
>    one
> 
> 2) Given two Enum values, iterate over the values between
>    them.
> 
> Did I miss those in the documentation or are they really
> missing?

Given the eg in the docs:
from enum import Enum
class Color(Enum):
    red = 1
    blue = 2
    green = 3

>>> Color(Color.red.value+1)
<Color.blue: 2>

>>> for i in range(Color.red.value,Color.green.value+1):
...   print (Color(i))
... 
Color.red
Color.blue
Color.green


If you say that is clunky I wont argue :-)

[toc] | [prev] | [next] | [standalone]


#106934

FromMarko Rauhamaa <marko@pacujo.net>
Date2016-04-13 13:58 +0300
Message-ID<87d1ptu665.fsf@elektro.pacujo.net>
In reply to#106933
Rustom Mody <rustompmody@gmail.com>:

> Given the eg in the docs:
> from enum import Enum
> class Color(Enum):
>     red = 1
>     blue = 2
>     green = 3
>
>>>> Color(Color.red.value+1)
> <Color.blue: 2>

But:

   >>> class Color(enum.Enum):
   ...   red = 0xff0000
   ...   green = 0x00ff00
   ...   blue = 0x0000ff
   ...
   >>> Color(Color.red.value + 1)
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
     File "/usr/lib64/python3.4/enum.py", line 222, in __call__
       return cls.__new__(cls, value)
     File "/usr/lib64/python3.4/enum.py", line 457, in __new__
       raise ValueError("%r is not a valid %s" % (value, cls.__name__))
   ValueError: 16711681 is not a valid Color

I take it that enums in Python are identifiers only. While you can
iterate over all enums (and the definition order is preserved), it is
simply a way to operate on *all* enums.

So the answer to the OP's question is: that feature is not supported.


Marko

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web