Python 使用 enum

一、定义class模拟

1

如果是新版Python用户(Python 3.4 with PEP 435):

from enum import Enum
Animal = Enum('Animal', 'ant bee cat dog')

2
class Directions:

up = 0
down = 1
left = 2
right =3

value1 = Directions.up

3

def enum(**enums):

return type('Enum', (), enums)

Numbers = enum(ONE=1, TWO=2, THREE='three')
# Numbers.ONE == 1, Numbers.TWO == 2 and Numbers.THREE == 'three'

posted @ 2016-08-05 22:07  JustRelax  阅读(211)  评论(0编辑  收藏  举报