Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

not all elements appear when iterating through an Enum in python 3

in the code below, i assign b to function f in an Enum. when i iterate through that enum, however, b does not appear, although i can still access it via E.b. does anyone know what's going on here? is this just a bug? i'm using python 3.5.1.

In [42]: from enum import Enum
In [43]: def f(): pass
In [44]: class E(Enum):
    ...:     a = 4
    ...:     b = f
    ...:     c = 5
    ...:
In [45]: list(E)
Out[45]: [<E.a: 4>, <E.c: 5>]
In [46]: E.b
Out[46]: <function __main__.f>
like image 672
acushner Avatar asked Jan 05 '23 17:01

acushner


1 Answers

Descriptors don't become members of the enumeration. If you give your enum behavior, those methods live in the same namespace as the values themselves, so this is the only way for enum to tell them apart.

  • Allowed members and attributes of enumerations¶
  • _EnumDict.__setitem__
like image 73
Josh Lee Avatar answered Jan 07 '23 07:01

Josh Lee