Python装饰器之classmethod 和 staticmethod:

    1. class A(object):
    2. arg1 = 1
    3. def __init__(self,user,pwd):
    4. self.user = user
    5. self.pwd = pwd
    6. def show(self):
    7. print "user -> ",self.user
    8. print "pwd -> ",self.pwd
    9. @classmethod
    10. def getConn(cls,_cstr):
    11. sp = _cstr.split(';')
    12. newA = cls(sp[0],sp[1])
    13. #newA.show()
    14. return newA
    15. @staticmethod
    16. def staticFun():
    17. print "run static fun "
    18. aa = A.getConn('zhangshang;newtest')
    19. aa.show()
    20. A.staticFun()
    21. aa.staticFun()

    Python 生成器之yield
    example: 使用生成器深度遍历目录:

    1. import os
    2. def find(path):
    3. if not os.path.isdir(path):
    4. return
    5. _files = os.listdir(path)
    6. for _f in _files:
    7. if os.path.isdir(os.path.join(path,_f)):
    8. for item in find(os.path.join(path,_f)):
    9. yield item
    10. else:
    11. yield _f
    12. path = r'D:\'
    13. f = find(path)
    14. for _f in f:
    15. print _f