惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
D
Darknet – Hacking Tools, Hacker News & Cyber Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
G
Google Developers Blog
MyScale Blog
MyScale Blog
Google DeepMind News
Google DeepMind News
J
Java Code Geeks
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Check Point Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
P
Proofpoint News Feed
D
Docker
Jina AI
Jina AI
博客园 - 三生石上(FineUI控件)
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
Google DeepMind News
Google DeepMind News
L
LINUX DO - 最新话题
T
Tailwind CSS Blog
N
Netflix TechBlog - Medium
Forbes - Security
Forbes - Security
MongoDB | Blog
MongoDB | Blog
Attack and Defense Labs
Attack and Defense Labs
Webroot Blog
Webroot Blog
A
About on SuperTechFans
Schneier on Security
Schneier on Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
Microsoft Azure Blog
Microsoft Azure Blog
F
Fortinet All Blogs
IT之家
IT之家
The Last Watchdog
The Last Watchdog
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog
Project Zero
Project Zero
B
Blog
Recorded Future
Recorded Future
博客园_首页
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
SegmentFault 最新的问题
Security Archives - TechRepublic
Security Archives - TechRepublic
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Hacker News: Front Page
T
Threatpost
H
Heimdal Security Blog
Cloudbric
Cloudbric
Google Online Security Blog
Google Online Security Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog

博客园 - tsembrace

Pandas数据分析教程,由浅入深讲解(五)Pandas IO操作 Pandas数据分析教程,由浅入深讲解(四) Pandas数据分析教程,由浅入深讲解(三) Pandas数据分析教程,由浅入深讲解(二) Pandas数据分析教程,由浅入深讲解(一) B站黑马Python+AI零基础入门(二)AI应用-实战 B站黑马Python+AI零基础入门(一)AI应用-基础 B站清华128小时Python高级教程一(C3深入类和对象) B站清华128小时Python高级教程一(C1、C2) B站python入门学习---第二阶段第二章数据库、SQL和MySQL B站python入门学习---第二阶段第一章类和对象 B站python入门学习---第一阶段第十章 数据可视化 B站python入门学习---第一阶段第八~九章 文件操作、异常、模块与包 B站python入门学习---第一阶段前七章回顾复习(二) B站python入门学习---第一阶段前七章回顾复习(一) Python入门学习(十)第十四章其他 Python入门学习(九)Python的高级语法与用法(四)装饰器 Python入门学习(九)Python的高级语法与用法(三)函数式编程 Python入门学习(九)Python的高级语法与用法(二)闭包 Python入门学习(八)JSON、反序列化、序列化 Python入门学习(七)高级部分:正则表达式(二) Python入门学习(七)高级部分:正则表达式 Python入门学习(六)高级部分:面向对象 Python入门学习(五)函数 Python入门学习(四)流程控制 包与模块
Python入门学习(九)Python的高级语法与用法(一)枚举
tsembrace · 2025-08-08 · via 博客园 - tsembrace

 枚举一般包含一组有所关联的成员,每个成员被指定一个整型数字;可以定义枚举类型中的成员,分别代表一组相关却有不同含义的内容。

一般成员定义会依次为0\1\2...当然也不是完全必须遵循这个规则。

from enum import Enum

#枚举本身也是一个类
class VIP(Enum):
    YELLOW = 1
    GREEN = 2
    BLACK = 3
    RED = 4

print(VIP.YELLOW)
"""
yellow = 1
green = 2

dic = {"yellow" : 1, "green" : 2}

class TypeDiamond():
    yellow = 1
    green = 2
"""
#如用上述三种方法定义类型,无法控制其可变,而枚举是不能被轻易更改的
#而且没有防止相同标签的功能

from enum import Enum

#枚举本身也是一个类
class VIP(Enum):
    YELLOW = 1
    GREEN = 2
    BLACK = 3
    RED = 4

print(VIP.YELLOW)
#下面代码这里会保护内容不被更改,会报错
#VIP.YELLOW = 6
from enum import Enum

#枚举本身也是一个类
class VIP(Enum):
    YELLOW = 1
    GREEN = 2
    BLACK = 3
    RED = 4

#获取类型的数值
print(VIP.BLACK.value)

#获取枚举类型名
print(VIP.GREEN.name)
print(type(VIP.GREEN.name))   #是一个字符串
print(VIP.GREEN)  #获取枚举类型
print(type(VIP.GREEN))   #是一个枚举类
print(VIP['GREEN'])  #获取枚举类型的另一个方法


#枚举也是可以遍历的
for v in VIP:
    print(v)

#枚举之间的比较

from enum import Enum

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

#枚举类型的比较

result = VIP.YELLOW== VIP.GREEN
print(result)
result = VIP.YELLOW== 1
print(result)
result = VIP.YELLOW.value== 1
print(result)
#但不能直接做大小比较,会报错
#result = VIP.YELLOW> VIP.GREEN
#需要用枚举类型的值才可以比大小
result = VIP.YELLOW.value == VIP.PINK.value

print(result)

class VIP1(Enum):
    YELLOW = 1
    GREEN = 2
    BLACK = 3
    RED = 4
    PINK = 1    #实际与YELLOW是一类,可以看为其别名

#枚举类之间的比较
#显然不等
result = VIP==VIP1
print(result)
#显然不等
result = VIP.YELLOW==VIP1.YELLOW
print(result)
#TRUE
result = VIP.YELLOW.value ==VIP1.YELLOW.value
print(result)

#别名与遍历

from enum import Enum

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


for v in VIP:
    print(v)       #不会输出PINK这种别名的枚举类型
    print(v.value)

#如果需要把别名遍历出来
for v in VIP.__members__:
    print(v)