Created
August 2, 2017 06:36
-
-
Save kunpengku/e241e36616f63844fc880833d3e2509f to your computer and use it in GitHub Desktop.
enum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding:utf8 | |
from enum import Enum | |
class Color(Enum): | |
red=1 | |
green=2 | |
black=3 | |
#init 用值实例化一个 enum | |
c1 = Color(2) | |
print(c1) | |
print(c1.name) | |
print(c1.value) | |
# 如果知道name,如果获取对应的 enum呢 | |
s = 'black' | |
c2 = getattr(Color, s) | |
print(c2) | |
print(c2.name) | |
print(c2.value) | |
# | |
# Color.red 也是一个对象,所以可以 动态添加 attribute | |
Color.red.price=30.0 | |
Color.black.price=40.0 | |
Color.green.price=40.0 | |
Color.green.xxx = True | |
print(c2.price) | |
print(c1.xxx) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment