笔记-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

 

转载于:https://www.cnblogs.com/wodeboke-y/p/10989291.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值