2

【笔记】Python枚举

 1 year ago
source link: https://loli.fj.cn/2022/12/26/Python%E6%9E%9A%E4%B8%BE/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

【笔记】Python枚举

2022-12-26

在Python3.4以后引入了枚举模块,可以创建一个枚举类

from enum import Enum

class 枚举名(Enum):
类型名 = 类型值

使用枚举定义的类型值

  • 通过类型名获取类型值
import 枚举名

枚举名.类型名.value
枚举名['类型名'].value

自定义枚举的属性

定义枚举的属性

from enum import Enum

class 枚举名(Enum):
类型名 = 类型值

枚举名.类型名.属性名 = 属性值

使用枚举的属性

import 枚举名

枚举名.类型名.属性名

哔哩哔哩——图灵学院教程


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK