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


Groups > comp.lang.python > #106934

Re: Enum questions.

From Marko Rauhamaa <marko@pacujo.net>
Newsgroups comp.lang.python
Subject Re: Enum questions.
Date 2016-04-13 13:58 +0300
Organization A noiseless patient Spider
Message-ID <87d1ptu665.fsf@elektro.pacujo.net> (permalink)
References <570E1B98.4080904@rece.vub.ac.be> <mailman.67.1460542399.15650.python-list@python.org> <8e3c705a-6d73-49fa-abcf-8b1197a83e5e@googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

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

csiph-web