• Python枚举类


    本篇主要介绍Python中枚举类的用法,更多内容请参考:Python学习指南

    当我们需要定义常量时,一个办法就是用大写变量通过整数来定义,例如月份:

    JAN = 1
    FEB = 2
    MAR = 3
    ···
    NOV = 11
    DEC = 12
    

    好处就是简单,确定是类型是int,并且仍然是变量。

    更好的办法是为这样的枚举类型定义一个class类型,然后,每个常量都是class的一个唯一实例。Python提供了Enum来来实现这个功能:

    from enum import Enum
    
    Month = Enum('Month', ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'))
    

    这样我们就获得了Month类型的枚举值,可以直接使用Month.Jan来引用一个常量,或者枚举它的所有成员:

    for name, member in Month.__members__.items():
        print(name, '=>', member, ',', member.value)
    

    value属性则是自动赋给成员int常量,默认从1开始计数。

    如果需要更精确地控制枚举类型,可以从Enum派生出自定义类:

    @unique
    class Weekday(Enum):
        Sun = 0
        Mon = 1
        Tue = 2
        Wed = 3
        Thu = 4
        Fri = 5
        Sat = 6
    

    @unique装饰器可以帮助我们检查保证没有重复值

    访问这些枚举类型可以有若干种方法:

    >>> day1 = Weekday.Mon
    >>> print(day1)
    Weekday.Mon
    >>> print(Weekday.Tue)
    Weekday.Tue
    >>> print(Weekday['Tue'].name)
    Tue
    >>> print(Weekday.Tue.value)
    2
    >>> print(day1 == Weekday.Mon)
    True
    >>> print(day1 == Weekday.Tue)
    False
    >>> print(Weekday(1))
    Weekday.Mon
    >>> print(day1 == Weekday(1))
    True
    >>> Weekday(7)
    Traceback (most recent call last):
      ...
    ValueError: 7 is not a valid Weekday
    >>> for name, member in Weekday.__members__.items():
    ...     print(name, '=>', member)
    ...
    Sun => Weekday.Sun
    Mon => Weekday.Mon
    Tue => Weekday.Tue
    Wed => Weekday.Wed
    Thu => Weekday.Thu
    Fri => Weekday.Fri
    Sat => Weekday.Sat
    
  • 相关阅读:
    day25:接口类和抽象类
    vue1
    How the weather influences your mood?
    机器学习实验方法与原理
    How human activities damage the environment
    Slow food
    Brief Introduction to Esports
    Massive open online course (MOOC)
    Online learning in higher education
    Tensorflow Dataset API
  • 原文地址:https://www.cnblogs.com/miqi1992/p/8372091.html
Copyright © 2020-2023  润新知