笔记-python-lib—data types-enum
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了笔记-python-lib—data types-enum,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3710字,纯文字阅读大概需要6分钟。
内容图文
笔记-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
原文:https://www.cnblogs.com/wodeboke-y/p/10989291.html
内容总结
以上是互联网集市为您收集整理的笔记-python-lib—data types-enum全部内容,希望文章能够帮你解决笔记-python-lib—data types-enum所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。