您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

python tips

2025/3/7 20:48:38发布18次查看
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
python代码
s='123456789' s.isdigit()#return true
3、list取交集
python代码
s=[1,2,3] w=[2,3,4] list(set(s).intersection(w))
4、两个list转成一个dict
python代码
dict(zip(a,b))
5、singleton
python代码
def singleton(cls): instances = {} def get_instance(): if cls not in instances: instances[cls] = cls() return instances[cls] return get_instance
第二种tornado ioloop中使用的单例模式:
python代码
@staticmethod def instance(): """returns a global ioloop instance. most single-threaded applications have a single, global ioloop. use this method instead of passing around ioloop instances throughout your code. a common pattern for classes that depend on ioloops is to use a default argument to enable programs with multiple ioloops but not require the argument for simpler applications:: class myclass(object): def __init__(self, io_loop=none): self.io_loop = io_loop or ioloop.instance() """ if not hasattr(ioloop, "_instance"): with ioloop._instance_lock: if not hasattr(ioloop, "_instance"): # new instance after double check ioloop._instance = ioloop() return ioloop._instance
6、list排重
python代码
{}.fromkeys(list).keys()
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product