Python - 如何通过索引获取枚举值

Sar*_*oli 7 python enums

我有一个 Python 中的 days_of_the week 的枚举:

class days_of_the_week(str, Enum):
  monday = 'monday'
  tuesday = 'tuesday'
  wednesday = 'wednesday'
  thursday = 'thursday'
  friday = 'friday'
  saturday = 'saturday'
  sunday = 'sunday'
Run Code Online (Sandbox Code Playgroud)

我想使用索引访问该值。

我试过了:

days_of_the_week.value[index]
Run Code Online (Sandbox Code Playgroud)
days_of_the_week[index].value
Run Code Online (Sandbox Code Playgroud)
days_of_the_week.values()[index]
Run Code Online (Sandbox Code Playgroud)

等等...但是我尝试的所有内容都没有返回枚举的值(例如days_of_the_week[1] >>> 'tuesday')

有办法吗?

not*_*hal 8

IIUC,你想做的事:

from enum import Enum

class days_of_the_week(Enum):
    monday = 0
    tuesday = 1
    wednesday = 2
    thursday = 3
    friday = 4
    saturday = 5
    sunday = 6

>>> days_of_the_week(1).name
'tuesday'
Run Code Online (Sandbox Code Playgroud)