Python基础语法18--枚举定义和访问

目录

一、枚举:

二、enum模块

1、枚举的定义

2、枚举成员及属性的访问

3、枚举的迭代

三、枚举使用注意说明

四、使用普通类直接实现枚举,存在的问题


一、枚举:

  • 枚举可看作是一系列符号名称的集合,集合中每一个元素要保证唯一性和不可变,因此我们可以对枚举中元素进行恒等比较,通俗来讲枚举就是一系列常量的集合,枚举是可迭代的.
  • 枚举就有不可变的特性,所以枚举的主要作用就是用来定义常量的。

二、enum模块

enum模块是系统内置模块,不建议使用import enum将enum模块中的所有数据都导入,一般使用的最多的就是enum模块中的Enum、IntEnum、unique这几项,建议按照如下进行导入:

# 导入枚举类
from enum import Enum
from enum import IntEnum  # 限制枚举类中的Value只能是整型数字
from enum import Enum, unique  # unique,限制枚举类中的key不能相同

1、枚举的定义

可以通过继承 Enum 的方式来实现;

举例:定义枚举类color和color2

# 导入枚举类
from enum import Enum

# 继承枚举类
class color(Enum):
    YELLOW = 1
    BEOWN = 1
    # 注意BROWN的值和YELLOW的值相同,这是允许的,此时的BROWN相当于YELLOW的别名
    RED = 2
    GREEN = 3
    PINK = 4

class color2(Enum):
    YELLOW = 1
    RED = 2
    GREEN = 3
    PINK = 4

2、枚举成员及属性的访问

# 枚举成员
print(color.YELLOW)
# 枚举成员名称
print(color.YELLOW.name)
# 枚举成员值
print(color.YELLOW.value)

输出结果:

各种使用枚举类举例:

print(color.YELLOW)  # color.YELLOW
print(type(color.YELLOW))  # <enum 'color'>

print(color.YELLOW.value)  # 1
print(type(color.YELLOW.value))  # <class 'int'>

print(color.YELLOW == 1)  # False
print(color.YELLOW.value == 1)  # True
print(color.YELLOW == color.YELLOW)  # True
print(color.YELLOW == color2.YELLOW)  # False
print(color.YELLOW is color2.YELLOW)  # False
print(color.YELLOW is color.YELLOW)  # True

print(color(1))  # color.YELLOW
print(type(color(1)))  # <enum 'color'>

打印结果:

3、枚举的迭代

# 导入枚举类
from enum import Enum

# 继承枚举类
class color(Enum):
    YELLOW = 1
    RED = 2
    GREEN = 3

# 枚举的迭代:方式 1
for co in color:
    # 枚举成员
    print(co)
    # 枚举成员名称
    print(co.name)
    # 枚举成员值
    print(co.value)

print('我是分割线------------------')

# 枚举的迭代:方式 2
print(list(color))

输出结果:

三、枚举使用注意说明

1、枚举类不能用来实例化对象;

2、访问枚举类中的某一项,直接使用类名访问加上要访问的项,如color.YELLOW

3、枚举类里面定义的Key = Value,在类外部不能修改Value值,否则会报错

color.YELLOW = 2  # Wrong, can't reassign member

4、枚举成员及属性可以使用 is 进行对象比较,还可以使用 == 进行值比较;但是枚举成员不能进行大小比较,如不能进行以下比较,会报错:TypeError: '<' not supported between instances of 'color' and 'color'

color.YELLOW < color.RED

5、导入Enum之后,一个枚举类中的Key和Value,Key不能相同,Value可以相同,但是Value相同的各项Key都会当做别名

6、如果要枚举类中的Value只能是整型数字,导入IntEnum,然后继承IntEnum;注意,如果value为字符串的数字,也不会报错

# 导入枚举类
from enum import IntEnum

# 继承枚举类
class color(IntEnum):
    YELLOW = "1"
    BEOWN = 1
    # 注意BROWN的值和YELLOW的值相同,这是允许的,此时的BROWN相当于YELLOW的别名
    RED = 2
    GREEN = 3
    PINK = 4

print(color.YELLOW.value)  # 1
print(type(color.YELLOW.value))  # <class 'int'>
print(color(1))

打印结果:

7、定义枚举时,成员名称是不可以重复的,但成员值是可以重复的,如果想要保证成员值value不可重复,需要导入unique函数,通过装饰器 @unique 来实现

# 导入枚举类
from enum import Enum, unique

# 继承枚举类
@unique
class color(Enum):
    YELLOW = 1
    PINK = 1

print(color.YELLOW)

输出报错:

ValueError: duplicate values found in <enum 'color'>: PINK -> YELLOW

四、使用普通类直接实现枚举,存在的问题

1、枚举类中,不应该存在key相同的枚举项(类变量);

-- 普通类中,可以定义相同的key,但是值不同,会覆盖前面的key值;如:增加YELLOW = 3;

2、不允许在类外直接修改枚举项的值;

-- 普通类,可以在类外直接修改key值;如在类定义外赋值color.YELLOW = 99;

class color():
    YELLOW = 1
    RED = 2
    GREEN = 3
    PINK = 4

# 访问枚举项
print(color.YELLOW)  # 打印结果:1

猜你喜欢

转载自blog.csdn.net/nikeylee/article/details/129830356
今日推荐