Python的学习笔记--枚举
枚举
在python
中,枚举使用Enum
模块。 具体使用如下:
1 |
|
在枚举中可以自动为下面的变量赋值,我们可以使用迭代把数据显示出来:
1 | from enum import Enum |
打印结果:
1 | Mon : Week.Mon , 1 |
如果像自定义枚举的值,可以使用继承的方法,自定义一个枚举:
1 | from enum import Enum, unique |
打印结果:
1 | Sun : Week.Sun , 0 |
可以使用@unique
装饰器来保证值的唯一。
1 | from enum import Enum, unique |
上面的代码会报错:TypeError: Attempted to reuse key: 'Sat'
对于枚举的使用可以用Week.Sun
取枚举的值,Week.Sun.value
来取对应的整型的值。
Python的学习笔记--枚举
http://blog.laofu.online/2019/03/21/2019-03-22-python-gaoji/
You need to set
install_url
to use ShareThis. Please set it in _config.yml
.