笔记-python-lib—data types-enum
笔记-python-lib—data types-enum
1. enum
Source code: Lib/enum.py
文档:https://docs.python.org/3/library/enum.html#using-auto
枚举类型enum是比较重要的一个数据类型,它是一种数据类型而不是数据结构,我们通常将一组常用的常数声明成枚举类型方便后续的使用。当一个变量有几种可能的取值的时候,我们将它定义为枚举类型。
1.1. Module Contents
This module defines four enumeration classes that can be used to define unique sets of names and values: Enum, IntEnum, Flag, and IntFlag. It also defines one decorator, unique(), and one helper, auto.
class enum.Enum
Base class for creating enumerated constants. See section Functional API for an alternate construction syntax.
基础类的枚举类,最常用。
class enum.IntEnum
Base class for creating enumerated constants that are also subclasses of int.
class enum.IntFlag
Base class for creating enumerated constants that can be combined using the bitwise operators without losing their IntFlag membership. IntFlag members are also subclasses of int.
class enum.Flag
Base class for creating enumerated constants that can be combined using the bitwise operations without losing their Flag membership.
enum.unique()
Enum class decorator that ensures only one name is bound to any one value.
装饰器,用于保证枚举类中属性值的唯一性;
class enum.auto
Instances are replaced with an appropriate value for Enum members.
1.2. 基础使用
创建enum类
class Gender(Enum):
a = 1
b = 2
c = 3
d = 4
e = 5
pr_type(Gender)
pr_type(Gender(1))
pr_type(Gender.b)
<enum 'Gender'> <class
'enum.EnumMeta'>
Gender.a <enum 'Gender'>
Gender.b <enum 'Gender'>
Gender就是一个类,它的类型是enum.EnumMeta
<enum 'Gender'>are enumeration
members (or enum members) and are functionally constants.是enum members,也叫功能常数
可以通过Gender.a或者Gender(1)或Gender[‘a’]引用属性
每个枚举类有两个属性:name,value,对应的是类变量中的属性名和值。
使用构造方法创建类:
from string import ascii_lowercase
ex1 = Enum('ex', (list(ascii_lowercase)))
pr_type(ex1)
print(ex1(3).__repr__())
结果:
<enum 'ex'> <class
'enum.EnumMeta'>
<ex.c: 3>
使用Enum()函数(就是Enum的构造方法)创建枚举类,该构造方法的第一个参数是枚举类的类名;第二个参数是一个元组,用于列出所有枚举值,也可以是可迭代对象;
本例中未给出枚举类的值,这时它会默认从1开始自增。
1.3.
属性重复
枚举类的enum members不能重复
class S(Enum):
sq = 2
sw = 3
sq = 4
s = S
报错:
TypeError: Attempted to reuse key: 'sq'
但值可以重复:
class S(Enum):
sq = 2
sw = 3
#sq = 4
sr = 3
s = S
没有报错。
如果不允许值重复,enum有一个装饰器unique:
@enum.unique
class S(Enum):
sq = 2
sw = 3
#sq = 4
sr = 3
s = S
这里产生抛出了一个异常:
ValueError: duplicate values found in
<enum 'S'>: sr -> sw
1.4.
其它操作
案例对象:
class Shape(Enum):
SQUARE = 2
DIAMOND = 1
CIRCLE = 3
ALIAS_FOR_SQUARE = 2
1.4.1.
iteration
>>> list(Shape)
[<Shape.SQUARE: 2>,
<Shape.DIAMOND: 1>, <Shape.CIRCLE: 3>]
The special attribute __members__ is
an ordered dictionary mapping names to members. It includes all names defined
in the enumeration, including the aliases:
>>>
for name, member in Shape.__members__.items():
... name, member
1.4.2.
planet
枚举类也可能自定义构造和生成方式,下面是一个简单的演示:
If __new__() or __init__() is
defined the value of the enum member will be passed to those methods:
>>>
class Planet(Enum):
... MERCURY = (3.303e+23, 2.4397e6)
... VENUS = (4.869e+24, 6.0518e6)
... EARTH = (5.976e+24, 6.37814e6)
... MARS = (6.421e+23, 3.3972e6)
... JUPITER = (1.9e+27, 7.1492e7)
... SATURN = (5.688e+26, 6.0268e7)
... URANUS = (8.686e+25, 2.5559e7)
... NEPTUNE = (1.024e+26, 2.4746e7)
... def __init__(self, mass, radius):
... self.mass = mass # in kilograms
... self.radius = radius # in meters
... @property
... def surface_gravity(self):
... # universal gravitational constant (m3 kg-1 s-2)
... G = 6.67300E-11
... return G * self.mass / (self.radius * self.radius)
...
>>>
Planet.EARTH.value
(5.976e+24,
6378140.0)
>>>
Planet.EARTH.surface_gravity
9.802652743337129
笔记-python-lib—data types-enum的更多相关文章
- Python - 2. Built-in Collection Data Types
From: http://interactivepython.org/courselib/static/pythonds/Introduction/GettingStartedwithData.htm ...
- Python - 1. Built-in Atomic Data Types
From:http://interactivepython.org/courselib/static/pythonds/Introduction/GettingStartedwithData.html ...
- 高性能MySQL笔记-第4章Optimizing Schema and Data Types
1.Good schema design is pretty universal, but of course MySQL has special implementation details to ...
- ExtJS笔记 Ext.data.Types
This is a static class containing the system-supplied data types which may be given to a Field. Type ...
- Data Types in the Kernel <LDD3 学习笔记>
Data Types in the Kernel Use of Standard C Types /* * datasize.c -- print the size of common data it ...
- 数据分析---《Python for Data Analysis》学习笔记【04】
<Python for Data Analysis>一书由Wes Mckinney所著,中文译名是<利用Python进行数据分析>.这里记录一下学习过程,其中有些方法和书中不同 ...
- 数据分析---《Python for Data Analysis》学习笔记【03】
<Python for Data Analysis>一书由Wes Mckinney所著,中文译名是<利用Python进行数据分析>.这里记录一下学习过程,其中有些方法和书中不同 ...
- 数据分析---《Python for Data Analysis》学习笔记【02】
<Python for Data Analysis>一书由Wes Mckinney所著,中文译名是<利用Python进行数据分析>.这里记录一下学习过程,其中有些方法和书中不同 ...
- 数据分析---《Python for Data Analysis》学习笔记【01】
<Python for Data Analysis>一书由Wes Mckinney所著,中文译名是<利用Python进行数据分析>.这里记录一下学习过程,其中有些方法和书中不同 ...
- 学习笔记之Python for Data Analysis
Python for Data Analysis, 2nd Edition https://www.safaribooksonline.com/library/view/python-for-data ...
随机推荐
- 09day vi命令详解
vi有三种模式(互相切换) 1. 命令模式 2. 插入模式(编辑模式) 3. 低行模式 三种模式的切换方法: 使用技巧 vi 文件信息 i --- 进入编辑模式 esc --- 退出编辑模式 :wq ...
- MySQL连接
INNER JOIN(内连接,或等值连接):获取两个表中字段匹配关系的记录. LEFT JOIN(左连接):获取左表所有记录,即使右表没有对应匹配的记录. RIGHT JOIN(右连接): 获取右表所 ...
- 修改eclipse工程jdk版本
在eclipse中项目jdk版本不匹配的时候需要修改项目工程的jdk版本,但是网上的一些版本修改不是很完全,经过一些摸索之后,参考总结了我在项目中的具体配置实践 问题: 修改eclipse中的项目jd ...
- 妙用python之编码转换
转自i春秋 文章难易度:★★ 知识点:python.编码转换 前 言 在日常渗透,漏洞挖掘,甚至是CTF比赛中,会遇到各种编码,常常伴随着这些编码之间的各种转换.记得刚入门那个时候,自己处理编码转换问 ...
- Python的几种主动结束程序方式
1. sys.exit() 执行该语句会直接退出程序,这也是经常使用的方法,也不需要考虑平台等因素的影响,一般是退出Python程序的首选方法. 该方法中包含一个参数status,默认为0,表示正常退 ...
- [PHP]新版的mongodb扩展安装和使用
旧版的mongo扩展已经不推荐使用了,在php7以上一般是安装和使用新版的mongodb扩展 ubuntu下 apt-get install php-mongodb 例如下面的代码进行了查询和插入集合 ...
- 攻防世界 misc Exercise 刷题记录
1.base64stego 1.zip伪加密 2. base64文件隐写,在网上找一个脚本
- "exit"未定义标签 问题
找了两个多小时,最后才发现是版本问题.因为是网上下的代码,可能用的版本比较高,而我自己的是2.4.10版本的opencv,所以正确的代码应该是如下: CV_Error(CV_StsBadArg,&qu ...
- yii2.0 构造函数
public function init() { parent:: init(); }
- 【PAT甲级】1089 Insert or Merge (25 分)(插入排序和归并排序)
题意: 输入一个正整数N(<=100),接着输入两行N个整数,第一行表示初始序列,第二行表示经过一定程度的排序后的序列.输出这个序列是由插入排序或者归并排序得到的,并且下一行输出经过再一次排序操 ...