- Base
from enum import Enum
Month = EnumEnum('Month', ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'))
$ Month.Jan.value => 1
$ Month.Jan.name => 'Jan'
- Defined
from enum import Enum, unique
# @unique 保证没有重复值
@unique
class Month(Enum):
Jan = 'January'
Feb = 'February'
Mar = 'March'
Apr = 'April'
May = 'May'
Jun = 'June'
Jul = 'July'
Aug = 'August'
Sep = 'September '
Oct = 'October'
Nov = 'November'
Dec = 'December'
$ Month.Jan.value => 'January'
$ Month.Jan.name => 'Jan'
- Sort
from enum import Enum
class User(Enum):
B = 98
A = 30
C = 12
$ print(', '.join(' ' + s.name for s in sorted(User)))
$ C, A, B