1、enum
Python代碼
#!/usr/bin/env python # -*- coding:utf-8 -*- def enum(**enums): return type('Enum', (), enums) Gender = enum(MALE=0,FEMALE=1) print Gender.MALE print Gender.FEMALE
2、檢查字串是否為number
Pyt
s='123456789' s.isdigit()#return True
4、兩個list轉成一個dict
Python代碼
s=[1,2,3] w=[2,3,4] list(set(s).intersection(w))
5、singleton
代碼
Pythonoopreee
dict(zip(a,b))
def singleton(cls): instances = {} def get_instance(): if cls not in instances: instances[cls] = cls() return instances[cls] return get_instance