python-枚举

直接上代码

from enum import Enum, unique


@unique
class DAY(Enum):
    """
    name和value都不可重复
    """
    MON: int = 1
    TUE: int = 2
    WED: int = 3
    THU: int = 4
    FRI: int = 5
    SAT: int = 6
    SUN: int = 0


if __name__ == '__main__':
    # 通过成员name来查询
    mon1 = DAY.MON
    print(mon)
    # 通过value查询
    mon2 = DAY(1)
    print(mon)

    print(mon1 is mon2)
    # true

    print(mon2.name)
    # 'MON'
    print(mon2.value)
    # 1

    # 可遍历
    for day in DAY:
        # <DAY.MON: 1>
        day = DAY.MON

posted @ 2021-10-18 18:05  做个笔记  阅读(26)  评论(0编辑  收藏  举报