Python: Enum枚举的实现

Python: Enum枚举的实现

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

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

class Animals(Enum):
    ant = 1
    bee = 2
    cat = 3
    dog = 4

旧版的python实现:

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'


你可能感兴趣的:(python)