python技巧31[python中使用enum][转]
以下几种方法来模拟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']
问题:有的时候需要使用enum的值作为字符串使用,像way5中表示的,大家有没有更好的办法?
参考:http://audbel.com/0/5009691
http://www.cnblogs.com/itech/archive/2011/03/08/1977245.html
python技巧31[python中使用enum][转]的更多相关文章
- Python学习-31.Python中集合的一些操作
add方法: s = {1,2,3} s.add(4) print(s)# {1, 2, 3, 4} 同list的append方法,若调用s.add(3),则不会有任何影响.这点与C#中的HashSe ...
- python技巧31[移植python2.x到3.x]
我们都知道python从2.x升级到3.x的过程中有一些不兼容的改动,但是有时还我们不得不将2.x的程序升级到3.x. 主要不兼容如下图: 移植过程: 1) 确保存在的代码有足够的测试覆盖.从2.x到 ...
- python技巧 计算字符串中字母出现的次数并取出最大
有一个字符串 “aaddfdfdercfghfyttefsfsfewretr123trefg5624sdfcgvfdgte6435234532”,现在需要取出里面出现次数最多的字符 第一种方法-装饰器 ...
- 《Python CookBook2》 第四章 Python技巧 - 若列表中某元素存在则返回之 && 在无须共享引用的条件下创建列表的列表
若列表中某元素存在则返回之 任务: 你有一个列表L,还有一个索引号i,若i是有效索引时,返回L[i],若不是,则返回默认值v 解决方案: 列表支持双向索引,所以i可以为负数 >>> ...
- python技巧 — Chrome浏览器中的 XPath Helper
用于XPath 爬取网页结构的时候使用, 安装后 快捷键调用 左边 ctrl+ shift+x 启动 安装流程: 1. 打开chrome浏览器,扩展程序 .搜索 XPath Helper 下载安装(前 ...
- python基础31[python IDE之Eclipse+PyDev]
一 入门IDE作为python的初学者,在语法和类库学习阶段,我们可以使用以下简单使用的IDE:1) Python SDK 自带的IDEL(Python GUI)2) Komodo-Edit3) No ...
- Python中模拟enum枚举类型的5种方法分享
这篇文章主要介绍了Python中模拟enum枚举类型的5种方法分享,本文直接给出实现代码,需要的朋友可以参考下 以下几种方法来模拟enum:(感觉方法一简单实用) 复制代码代码如下: # way1 ...
- Python IAQ中文版 - Python中少有人回答的问题
Python中少有人回答的问题 The Python IAQ: Infrequently Answered Questions 1 Q: 什么是"少有人回答的问题(Infrequently ...
- Python补充02 Python小技巧
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在这里列举一些我使用Python时积累的小技巧.这些技巧是我在使用Python过程 ...
随机推荐
- ADO接口简介
源地址:http://blog.csdn.net/xiaobai1593/article/details/7449151 参考: 1. 百度文库:http://wenku.baidu.com/view ...
- 趣味编程:CPS风格代码(Java 8,Functional Java版)
CPS风格代码(Java 8版) package fp; import java.util.function.IntConsumer; public class CPS { static int ad ...
- 在Eclipes中查看源代码和大纲快速定位
1 在Eclipes中查看源代码,快捷键使用clrl+光标,选择你要查看的方法和属性查看源代码.例如你想看StringBuilder这个类源代码 StringBuilder allow = new S ...
- python 本地变量和全局变量 locals() globals() global nonlocal 闭包 以及和 scala 闭包的区别
最近看 scala ,看到了它的作用域,特此回顾一下python的变量作用域问题. A = 10 B = 100 print A #10 print globals() #{'A': 10, 'B': ...
- python闭包和装饰器(转)
一.python闭包 1.内嵌函数 >>> def func1(): ... print ('func1 running...') ... def func2(): ... prin ...
- redis lua 用来传输日志
2.8 Lua Script Redis2.6内置的Lua Script支持,可以在Redis的Server端一次过运行大量逻辑,就像存储过程一样,避免了海量中间数据在网路上的传输. Lua自称是在S ...
- jenkins 自动构建gitlab项目
安装的plugin: - kubernetes:1.7.1 - workflow-aggregator:2.5 - workflow-job:2.21 - credentials-b ...
- 45. Jump Game II (Array; Two-Pointers,Greedy)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Distributing Ballot Boxes
Distributing Ballot Boxes http://acm.hdu.edu.cn/showproblem.php?pid=4190 Time Limit: 20000/10000 MS ...
- The Doors(几何+最短路,好题)
The Doors http://poj.org/problem?id=1556 Time Limit: 1000MS Memory Limit: 10000K Total Submissions ...