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

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Threat Research - Cisco Blogs
C
CERT Recently Published Vulnerability Notes
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
L
Lohrmann on Cybersecurity
D
Darknet – Hacking Tools, Hacker News & Cyber Security
K
Kaspersky official blog
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cisco Blogs
V
Vulnerabilities – Threatpost
L
LINUX DO - 热门话题
G
GRAHAM CLULEY
The GitHub Blog
The GitHub Blog
NISL@THU
NISL@THU
AWS News Blog
AWS News Blog
博客园 - 【当耐特】
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
阮一峰的网络日志
阮一峰的网络日志
Cyberwarzone
Cyberwarzone
V
V2EX
Know Your Adversary
Know Your Adversary
P
Palo Alto Networks Blog
月光博客
月光博客
MongoDB | Blog
MongoDB | Blog
Scott Helme
Scott Helme
A
Arctic Wolf
美团技术团队
S
Schneier on Security
P
Proofpoint News Feed
G
Google Developers Blog
The Hacker News
The Hacker News
S
Securelist
Microsoft Security Blog
Microsoft Security Blog
Project Zero
Project Zero
T
The Exploit Database - CXSecurity.com
量子位
T
Threatpost
Spread Privacy
Spread Privacy
Help Net Security
Help Net Security
B
Blog
WordPress大学
WordPress大学
B
Blog RSS Feed
J
Java Code Geeks
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Blog — PlanetScale
Blog — PlanetScale
Simon Willison's Weblog
Simon Willison's Weblog
Y
Y Combinator Blog
Cloudbric
Cloudbric

博客园 - 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)