Python装饰器之classmethod 和 staticmethod:
class A(object):
arg1 = 1
def __init__(self,user,pwd):
self.user = user
self.pwd = pwd
def show(self):
print "user -> ",self.user
print "pwd -> ",self.pwd
@classmethod
def getConn(cls,_cstr):
sp = _cstr.split(';')
newA = cls(sp[0],sp[1])
#newA.show()
return newA
@staticmethod
def staticFun():
print "run static fun "
aa = A.getConn('zhangshang;newtest')
aa.show()
A.staticFun()
aa.staticFun()
Python 生成器之yield
example: 使用生成器深度遍历目录:
import os
def 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 item
else:
yield _f
path = r'D:\'
f = find(path)
for _f in f:
print _f