Enum questions.

Marko Rauhamaa marko at pacujo.net
Wed Apr 13 06:58:10 EDT 2016


Rustom Mody <rustompmody at 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


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /