Python基础0228
2016-02-28 00:50
736 查看
python中函数是一等公民,即指在程序中可无限使用的对像。
Return an iterator yielding those items of iterable for which function(item) is true. If function is None, return the items that are true.
接收一个能返回布尔类型值的函数A和一个迭代器,迭代器中的值作为函数A的参数。如果函数A返回True,filter函数将会抛出这个值;如果返回None也认为是True,并抛出值。
Return True if bool(x) is True for any x in the iterable. If the iterable is empty, return False.
Make an iterator that computes the function using arguments from each of the iterables. Stops when the shortest iterable is exhausted.
函数的参数个数要和后面迭代器的个数相同,并以遍历完最短的迭代器作为结束。
可以根据给定外层函数的参数决定内层函数的行为。
构造一个和原来函数功能相同的新函数,减少参数数量,方便后续调用。
装饰器的本质就是一个函数,这个函数接受一个函数作为参数,但会返回一个函数,通常,返回的这个函数,是对传入的函数执行前后增加了一些语句,所以叫做装饰器。
携带参数的装饰器,就是多一层包装,外层函数根据参数决定返回的内层函数(装饰器)的行为。
装饰器外层函数对内层函数限定,确认内层函数执行的环境和条件
借助工具传递
高阶函数
函数作为参数
接收一个函数A作为调用函数B的参数。并根据函数A的值,决定函数B的行为。def sort(cmp, *args): ret = [] for item in args: for k, v in enumerate(ret): if cmp(item, v): ret.insert(k,item) #插入永远是插到对应元素前面 break else: ret.append(item) return ret def cmp1(x,y): return x>=y def cmp2(x,y): return x<=y
filter 过滤器
filter(function or None, iterable) --> filter objectReturn an iterator yielding those items of iterable for which function(item) is true. If function is None, return the items that are true.
接收一个能返回布尔类型值的函数A和一个迭代器,迭代器中的值作为函数A的参数。如果函数A返回True,filter函数将会抛出这个值;如果返回None也认为是True,并抛出值。
def biger(x): return x > 5 for i in filter(biger,range(10)): print(i) 6 7 8 9
any
any(iterable) -> boolReturn True if bool(x) is True for any x in the iterable. If the iterable is empty, return False.
map
map(func, *iterables) --> map objectMake an iterator that computes the function using arguments from each of the iterables. Stops when the shortest iterable is exhausted.
函数的参数个数要和后面迭代器的个数相同,并以遍历完最短的迭代器作为结束。
def inc(x): return x + 1 list(map(inc,[1,2,3,4])) [2, 3, 4, 5] def fold(fn, *args): it = iter(args) ret = next(it) # 利用生成器惰性求值,给ret赋初值,减少if判断。 for x in it: ret = fn(ret,x) return ret def add(x,y): return x + y fold(add,1,2,3,4) 10 def add_sun(fn,*args): ret = "" for i in args: if ret: ret = fn(ret,i) else: ret = i # 需要判断ret是否为空,给ret赋初值。 return ret def fn(x,y): return x + y print(add_sun(fn,1,2,3,4)) 10
函数作为返回值
def fun_1(x,y): return x * y def fun(x): def inner_fun(y): return x * y return inner_fun fun(2)(3)=fun_1(2,3) # 柯里化 def biger(x): def biger_fun(y): return y > x return biger_fun list(filter(biger(2),range(10))) [3, 4, 5, 6, 7, 8, 9]
可以根据给定外层函数的参数决定内层函数的行为。
partial
partial(func, *args, **keywords) - new function with partial application of the given arguments and keywords.构造一个和原来函数功能相同的新函数,减少参数数量,方便后续调用。
from functools import partial def many_args(a,b,c,x,y,z): print('a is {0}'.format(a)) print('b is {0}'.format(b)) print('c is {0}'.format(c)) print('x is {0}'.format(x)) print('y is {0}'.format(y)) print('z is {0}'.format(z)) callable(partial(many_args,x=1,y=2,z=3)) True # 定义关键字参数只能从后向前,默认按位置参数匹配 partial(many_args,x=1,y=2,z=3)(4,5,6) a is 4 b is 5 c is 6 x is 1 y is 2 z is 3
装饰器
import time def timeit(fn): def warp(*args,**kwargs): start = time.time() fn(*args,**kwargs) print(time.time() - start) return warp def sleep(x): time.sleep(x) timeit(sleep)(3) 3.0032060146331787 timeit(timeit(sleep))(3) 3.003615140914917 3.0036818981170654 @timeit def sleep(x): time.sleep(x) sleep(3) 3.001384973526001
装饰器的本质就是一个函数,这个函数接受一个函数作为参数,但会返回一个函数,通常,返回的这个函数,是对传入的函数执行前后增加了一些语句,所以叫做装饰器。
带参数的装饰器
import time def timeit(process_time=False): cacl = time.clock if process_time else time.time def inner_fun(fn): def wrap(*args,**kwargs): start = cacl() fn(*args,**kwargs) print(cacl() - start) return wrap return inner_fun def sleep(x): time.sleep(x) timeit()(sleep)(3) 3.0026581287384033 timeit(True)(sleep)(3) 0.0 @timeit(False) def sleep(x): time.sleep(x) sleep(3) 3.0025463104248047 def make_time(): def timeit(): def inner_time(fn): def wrap(*args,**kwargs): return fn(*args,**kwargs) return wrap return inner_time return timeit fun_1 = make_time() type(fun_1) function fun_1() <function __main__.make_time.<locals>.timeit.<locals>.inner_time> @fun_1() def fun_2(x): print(x) fun_2(3) 3
携带参数的装饰器,就是多一层包装,外层函数根据参数决定返回的内层函数(装饰器)的行为。
装饰器外层函数对内层函数限定,确认内层函数执行的环境和条件
装饰器对函数内置参数的传递
默认不传递借助工具传递
@wraps(fn)相当于将被装饰的函数 fn 的属性赋值给装饰器 wrap
匿名函数
lambdafor i in filter(lambda x:x>3,[1,2,3,8,2]): print(i) 8 lambda x,y:x >= y(1,2) <function __main__.<lambda>> # 匿名函数不能接受参数直接运行 test = lambda x,y:x >= y test(1,2) False
相关文章推荐
- 第二天 Python3.4.2 函数
- Python基础语法(Python基础知识点)
- python开发学习-day07(面向对象之多态、类的方法、反射、新式类and旧式类、socket编程)
- 六、keytool工具
- Python基础语法(Python基础知识点)
- python 写的一个爬虫程序源码
- Python中time模块和datetime模块的用法示例
- 简要讲解Python编程中线程的创建与锁的使用
- python3 装饰器
- 第二天 Python3.4.2 字符串的格式化 和 常用操作
- 基于Python的数据可视化 matplotlib seaborn pandas
- python中map()与zip()操作方法
- Notepad++插件Emmet不工作解决办法
- python之字符串
- python中input()与raw_input()的区别分析
- python PIL模块与随机生成中文验证码
- Pythont特殊语法filter,map,reduce,apply使用方法
- python常用模块os,random,sys
- 用Python做的数学四则运算_算术口算练习程序(后添加减乘除)
- win7+gvim7.4+spf13使用自动补全编写python脚本