파이썬 팁

巴扎黑
巴扎黑원래의
2016-12-09 13:27:142589검색

1. 열거형

파이썬 코드

#!/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. 문자열이 숫자인지 확인하세요

Python 코드

s='123456789'  
s.isdigit()#return True


3. 교차 목록

Python 코드

s=[1,2,3]  
w=[2,3,4]  
list(set(s).intersection(w))


4. 🎜>

파이썬 코드

dict(zip(a,b))


5. 싱글턴

파이썬 코드

def singleton(cls):  
    instances = {}  
    def get_instance():  
        if cls not in instances:  
            instances[cls] = cls()  
        return instances[cls]  
    return get_instance

토네이도 IOLoop에서 사용되는 두 번째 싱글턴 모드:

파이썬 코드

@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. 목록 재정렬

파이썬 코드

{}.fromkeys(list).keys()


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:redis-python다음 기사:redis-python