美文网首页
Python:14.枚举类

Python:14.枚举类

作者: 许瘦子来世 | 来源:发表于2018-07-12 09:32 被阅读3次
    # 枚举 Enum
    from enum import Enum,unique
    
    Month = Enum('Month', ('Jan', 'Feb', 'Mar', 'Apr', 'May'))
    for name, member in Month.__members__.items():
        print(name, '=>', member, ',', member.value)
    
    # 枚举派生自定义类
    @unique # 检查保证没有重复值
    class Weekday(Enum):
        Sun = 0 # Sun的Value被设定为0
        Mon = 1
        Tue = 2
        Wed = 3
        Thu = 4
        Fri = 5
        Sat = 6
    
    print(Weekday.Sun)
    print(Weekday['Sun'])
    print(Weekday(1))
    print(Weekday.Sun.value)
    

    相关文章

      网友评论

          本文标题:Python:14.枚举类

          本文链接:https://www.haomeiwen.com/subject/fudlpftx.html