• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

python tips

python 搞代码 4年前 (2022-01-09) 18次浏览 已收录 0个评论

1、enum

Python代码

#!/usr/bin/env python  # -*- coding:utf-8 -*-    def enum(**enums<a>本文来源gao*daima.com搞@代#码&网6</a>):      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()

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python tips

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址