圆月山庄资源网 Design By www.vgjia.com

以下几种方法来模拟enum:(感觉方法一简单实用)

复制代码 代码如下:
# way1
class Directions:
    up = 0
    down = 1
    left = 2
    right =3
   
print Directions.down

# way2
dirUp, dirDown, dirLeft, dirRight = range(4)

print dirDown

# way3
import collections
dircoll=collections.namedtuple('directions', ('UP', 'DOWN', 'LEFT', 'RIGHT'))
directions=dircoll(0,1,2,3)

print directions.DOWN

# way4
def enum(args, start=0):
    class Enum(object):
        __slots__ = args.split()

        def __init__(self):
            for i, key in enumerate(Enum.__slots__, start):
                setattr(self, key, i)

    return Enum()

e_dir = enum('up down left right')

print e_dir.down

# way5
# some times we need use enum value as string
Directions = {'up':'up','down':'down','left':'left', 'right':'right'}

print Directions['down']

标签:
Python,模拟enum数据

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com