标签:
函数参数可以设置缺省值
warning:The default value is evaluated only once. This makes a difference when the default is a mutable object such as list, dictionary, or instances of most classes.
def f(a, L=[]): L.append(a) return L print f(1), f(2), f(3) ------------------------- [1] [1, 2] [1, 2, 3] def f(a, L=None): if L is None: L = [] L.append(a) return L ----------------------- [1] [2] [3]
packing and unpacking
函数定义时:
*args表示接受到的参数会以tuples的形式呈现
**kwargs表示接受到的参数会以dictionary的形式呈现
函数调用时:
* : to unpack the arguments out of a list or tuple
**: to unpack the arguments out of dictionary
def hello(a, b): print a, b hello(*[2015, ‘hello‘]) ---------------------- 2015 hello def hello(a, b=‘world‘): print a, b hello(**{‘a‘:‘Hello‘, ‘b‘:‘World‘}) ---------------------- Hello World
函数式编程
把函数作为参数传入,这样的函数称为高阶函数,函数式编程就是指这种高度抽象的编程范式
lambda
(lambda x: x*2)(3)
装饰器 decorator
http://stackoverflow.com/questions/739654/how-can-i-make-a-chain-of-function-decorators-in-python
装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志、性能测试、事务处理, Web权限校验, Cache等。例如记录日志,需要对某些函数进行记录。笨的办法,每个函数加入代码,如果代码变了,就悲催了。装饰器的办法,定义一个专门日志记录的装饰器,对需要的函数进行装饰,搞定。
优点:抽离出大量函数中与函数功能本身无关的雷同代码并继续重用。即,可以将函数“修饰”为完全不同的行为,可以有效的将业务逻辑正交分解,如用于将权限和身份验证从业务中独立出来。
概括的讲,装饰器的作用就是为已经存在的对象添加额外的功能。
ps: remember decorators are called only once. Just when Python imports the script. You can‘t dynamically set the arguments afterwards. When you do "import x", the function is already decorated, so you can‘t change anything.
本质上,decorator就是一个返回函数的高阶函数
def log(func): def wrapper(*args, **kw): print ‘call %s():‘ % func.__name__ return func(*args, **kw) return wrapper @log #相当于now = log(now) def now(): print ‘2013-12-25‘ ------------------------------ >>> now() call now(): 2013-12-25 #如果decorator本身需要传入参数,那就需要编写一个返回decorator的高阶函数,写出来会更复杂。比如,要自定义log的文本: def log(text): def decorator(func): def wrapper(*args, **kw): print ‘%s %s():‘ % (text, func.__name__) return func(*args, **kw) return wrapper return decorator @log(‘execute‘) #相当于now = log(‘execute‘)(now) def now(): print ‘2013-12-25‘ ------------------------------ >>> now() execute now() 2013-12-25
有一些细节需要注意。装饰后(函数名等函数属性会发生改变)。所以写一个装饰器的时候,最好在实现之前加上functools模块的warp,它能保留原有函数的名称和docstring.
import functools def log(func): @functools.wraps(func) def wrapper(*args, **kw): print ‘call %s():‘ % func.__name__ return func(*args, **kw) return wrapper def log(text): def decorator(func): @functools.wraps(func) def wrapper(*args, **kw): print ‘%s %s():‘ % (text, func.__name__) return func(*args, **kw) return wrapper return decorator
有多个装饰器时,装饰器的顺序matters
def bread(func): def wrapper(): print "</‘‘‘‘‘‘\>" func() print "<\______/>" return wrapper def ingredients(func): def wrapper(): print "#tomatoes#" func() print "~salad~" return wrapper @bread @ingredients def sandwich(food="--ham--"): print food sandwich() #outputs: #</‘‘‘‘‘‘\> # #tomatoes# # --ham-- # ~salad~ #<\______/>
python内建map, reduce, filter, sorted函数的用法
map(f, [x1, x2, x3, x4]) = [f(x1), f(x2), f(x3), f(x4)] map(lambda x,y: x+y, [1,1,1], [2,3,4])
reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)
filter(f, [1, 2, 4, 5, 6, 9, 10, 15]) #筛选符合f的数据,返回list
sorted([x1, x2, x3, x4], f) #以f的形式进行排序,返回list
def str2int(s): def fn(x, y): return x * 10 + y def char2num(s): return {‘0‘: 0, ‘1‘: 1, ‘2‘: 2, ‘3‘: 3, ‘4‘: 4, ‘5‘: 5, ‘6‘: 6, ‘7‘: 7, ‘8‘: 8, ‘9‘: 9}[s] return reduce(fn, map(char2num, s)) #用lambda函数进一步简化 def char2num(s): return {‘0‘: 0, ‘1‘: 1, ‘2‘: 2, ‘3‘: 3, ‘4‘: 4, ‘5‘: 5, ‘6‘: 6, ‘7‘: 7, ‘8‘: 8, ‘9‘: 9}[s] def str2int(s): return reduce(lambda x,y: x*10+y, map(char2num, s))
2015-06-01
标签:
原文地址:http://www.cnblogs.com/whuyt/p/4543372.html