Python装饰器之classmethod 和 staticmethod:
class A(object):arg1 = 1def __init__(self,user,pwd):self.user = userself.pwd = pwddef show(self):print "user -> ",self.userprint "pwd -> ",self.pwd@classmethoddef getConn(cls,_cstr):sp = _cstr.split(';')newA = cls(sp[0],sp[1])#newA.show()return newA@staticmethoddef staticFun():print "run static fun "aa = A.getConn('zhangshang;newtest')aa.show()A.staticFun()aa.staticFun()
Python 生成器之yield
example: 使用生成器深度遍历目录:
import osdef find(path):if not os.path.isdir(path):return_files = os.listdir(path)for _f in _files:if os.path.isdir(os.path.join(path,_f)):for item in find(os.path.join(path,_f)):yield itemelse:yield _fpath = r'D:\'f = find(path)for _f in f:print _f
