匿名函数
In [1]: a = lambda x,y:x+y In [2]: a(1,2) Out[2]: 3
正则表达式
match如果第一个字符不匹配,则终止;search则是一个个字符里寻找。
In [3]: import re In [4]: re.search("hao","da jia hao") Out[4]: <_sre.SRE_Match at 0x7eff99911308> In [5]: re.search("hao","da jia h") In [6]: re.match("hao","da jia hao") In [7]: re.match("da","da jia hao") Out[7]: <_sre.SRE_Match at 0x7eff999115e0>
小括号中写上要输出的内容,结果以元组输出
In [10]: re.match("[0-9]{3}(go)","123go").groups() Out[10]: (‘go‘,)
单独输出
In [14]: re.match("[0-9]{3}(go)","123go").group(0) Out[14]: ‘123go‘ In [15]: re.match("[0-9]{3}(go)","123go").group(1) Out[15]: ‘go‘
In [29]: re.search("(go)","just go go go !").group(1) Out[29]: ‘go‘
模块导入导出
import module
from module import m1,m2,m3
import module_name as m4
通过sys.path可以查找python从目录中查找模块的顺序
In [31]: import sys In [32]: sys.path Out[32]: [‘‘, ‘/usr/bin‘, ‘/usr/lib64/python27.zip‘, ‘/usr/lib64/python2.7‘, ‘/usr/lib64/python2.7/plat-linux2‘, ‘/usr/lib64/python2.7/lib-tk‘, ‘/usr/lib64/python2.7/lib-old‘, ‘/usr/lib64/python2.7/lib-dynload‘, ‘/usr/lib64/python2.7/site-packages‘, ‘/usr/lib64/python2.7/site-packages/mysqlclient-1.3.13-py2.7-linux-x86_64.egg‘, ‘/usr/lib/python2.7/site-packages‘, ‘/usr/lib/python2.7/site-packages/setuptools-41.0.1-py2.7.egg‘, ‘/usr/lib/python2.7/site-packages/IPython/extensions‘, ‘/root/.ipython‘]
第一个目录为空,表示当前目录,即优先从当前目录查找
然后从/usr/bin目录中查找是否存在所导入的模块,依次排查剩下的所有目录
as通常用于同一文件中从不同模块引入的方法重名的时候,或者换一个更贴切业务含义的名字
In [34]: from re import search as sea In [35]: sea("ka","wa ka ka ") Out[35]: <_sre.SRE_Match at 0x7eff98800cc8>
装饰器
In [40]: def addLogging(func): ...: def log(*args, **kwargs): ...: print "%s is running"%func.__name__ ...: return func(*args) ...: return log ...: In [41]: def aa(): ...: print 123 ...: In [42]: aa = addLogging(aa) In [43]: aa() aa is running 123 In [44]: @addLogging ...: def bb(): ...: print 111 ...: In [45]: bb() bb is running 111
原文:https://www.cnblogs.com/perfei/p/11070601.html