标签:python
一.高阶函数:
顺序排序
enumerate([1,2 ,3 , 4, 5]) for idx, item in enumerate([1, 2, 3, 4]): print(idex) print(item) def sort(*args): ret = [] for item in args: for i, v in enumerate(ret): if item > v: ret.insert(i,item) break else: ret.append(item) return ret sort(3, 1, 2, 5)
逆序排列
def sort(f, *args): ret = [] for item in args: for i, v in enumerate(ret): if f: if item >= v: ret.insert(i,item) break else: if item <= v: ret.insert(i,item) break else: ret.append(item) return ret sort(True, 3, 1, 2, 5)
高阶函数
def sort(cmp, *args): ret = [] for item in args: for i, v in enumerate(ret): if cmp(item, v): ret.insert(i, item) break else: ret.append(item) return ret def cmp1(x, y): return x >= y def cmp2(x, y): return x <= y sort(cmp1, 3, 1, 2, 5) [5, 3, 2, 1]
二.特殊函数[内置函数]:
1.filter 根据布尔返回值,输出返回值为True的数值:
def bigger_5(x): if x > 5: return True return False filter(bigger_5, range(10)) list(filter(bigger_5, range(10))) >[6, 7, 8, 9]
2.map 输出函数中的返回值的一个迭代器:
格式:map( func, seq1[, seq2...] )
例子1:
def bigger_5(x): if x > 5: return True return False map(bigger_5, range(10)) list(map(bigger_5, range(10))) >[False, False, False, False, False, False, True, True, True, True]
例子2:
def inc(x): return x + 1 list(map(inc,[1, 2, 3, 4])) >[2, 3, 4, 5] list(map(inc,range(3))) >[1, 2, 3]
例子3:
list(map(lambda x:x + 1,range(3))) >[1, 2, 3]
例子4:
a = map(lambda x:x + 1,range(3)) next(a) >1 next(a) >2 next(a) >3 next(a) >StopIteration
3.reduce
格式:reduce( func, seq[, init] )
reduce函数即为化简,它是这样一个过程:每次迭代,将上一次的迭代结果(第一次时为init的元素,如没有init则为seq的第一个元素)与下一个元素一同执行一个二元的func函数。在reduce函数中,init是可选的,如果使用,则作为第一次迭代的第一个元素使用。
n = 5 print reduce(lambda x, y: x * y, range(1, n + 1)) # 120
m = 2 n = 5 print reduce( lambda x, y: x * y, range( 1, n + 1 ), m ) # 240
4.lambda
def sort(cmp, *args): ret = [] for item in args: for i, v in enumerate(ret): if cmp(item, v): ret.insert(i, item) break else: ret.append(item) return ret sort(lambda x, y: x >= y, 3, 1, 2, 5)
5.函数作为返回值
def make_ic(f=1): def inc(x): return x + f return inc inc1 = make_ic(1) inc1(5) >6
柯里化:
def bigger(x): def inner_bigger(y): return y > x return inner_bigger list(filter(bigger(5), range(10))) >[6, 7, 8, 9]
三.functools库
1.partial [设置默认参数, 后期任然可以通过关键字参数进行传参]
查看是不是可调用对象:
from functools import partial import pymysql callable(bigger_3) bigger_3 = partial(bigger,y=3) connect = partial(pymysql.connect, user=‘root‘, password=‘xxxx‘, database=‘xxxx‘, port=3306) connect(host=‘127.0.0.1‘)
四.装饰器
装饰器的本质就是一个函数,这个函数接收一个函数作为参数, 返回一个函数,通常,返回的这个函数,是对传入的函数执行前后增加了一些语句,所以叫做装饰;
普通装饰器
#第一种写法
def timeit(fn): def wrap(*args, **kwargs): start = time.time() ret = fn(*args,**kwargs) print(time.time() - start) return ret return wrap def sleep(x): time.sleep(x) timeit(sleep)(3)
第二种方法:
def timeit(fn): def wrap(*args, **kwargs): start = time.time() ret = fn(*args,**kwargs) print(time.time() - start) return ret return wrap @timeit def sleep(x): time.sleep(x) sleep(3)
time.time() //得到的是一个自然的时间 time.clock() //得到的是一个cpu执行时间,占用cpu的时间;
2.带参数的装饰器
import time def timeit(process_time=False): cacl = time.clock if process_time else time.time def inner_timeit(fn): def wrap(*args, **kwargs): start = cacl ret = fn(*args, **kwargs) print(cacl() - start) return ret return wrap return inner_timeit @timeit(True) def sleep(x): time.sleep(x) sleep(3)
import time def timeit(process_time=False): cacl = time.clock if process_time else time.time def inner_timeit(fn): def wrap(*args, **kwargs): start = cacl() ret = fn(*args, **kwargs) print(cacl() - start) return ret return wrap return inner_timeit def sleep(x): time.sleep(x) timeit(False)(sleep)(3) 等价 timeit()(sleep)(3) timeit(True)(sleep)(3)
import time def timeit(process_time=False): cacl = time.clock if process_time else time.time def inner_timeit(fn): def wrap(*args, **kwargs): start = cacl() ret = fn(*args, **kwargs) print(cacl() - start) return ret return wrap return inner_timeit @timeit(True) or @timeit() or @timeit(False) def sleep(x): time.sleep(x) sleep(3)
#带参数的装饰器的基本结构 def make_timeit(): def timeit(): def inner_timeit(fn): def wrap(*args, **kwargs): return fn(*args, **kwargs) return wrap return inner_timeit return timeit @make_timeit()() //这样写是有问题的,因为python的语法解析器只支持带一个参数 正确写法如下 time_2 = make_timeit() @time_2() 或者 time_2 = make_timeit()() @time_2 def fn(): pass
3.装饰器的实际应用:
flask使用了大量的装饰来做路由
[flask.pocoo.org]
#利用装饰器进行权限控制
def check(allows): def deco(fn): def wrap(username, *args, **kwargs): if username in allows: return fn(username, *args, **kwargs) //这一行需要和wrap这一行一一对应起来 return "not allow" return wrap return deco @check(["comyn", "mage"]) def private(username): print("congratulation") private("comyn")
4.函数的字文档[wraps]:
def test(): ‘‘‘ this is test @return None ‘‘‘ pass help(test) >Help on function test in module __main__: test() this is test @return None import time
def timeit(process_time=False): cacl = time.clock if process_time else time.time def inner_timeit(fn): def wrap(*args, **kwargs): start = cacl() ret = fn(*args, **kwargs) print(cacl() - start) return ret wrap.__name__ = fn.__name__ wrap.__doc__ = fn.__doc__ return wrap return inner_timeit @timeit(True) or @timeit() or @timeit(False) def sleep(x): ‘‘‘ this is test ‘‘‘ sleep.__name__ help(sleep)
# 使用wraps实现函数的字文档
import time from functools import wraps def timeit(process_time=False): cacl = time.clock if process_time else time.time @wraps() def inner_timeit(fn): def wrap(*args, **kwargs): start = cacl() ret = fn(*args, **kwargs) print(cacl() - start) return ret return wrap return inner_timeit @timeit(True) or @timeit() or @timeit(False) def sleep(x): ‘‘‘ this is test ‘‘‘ sleep.__name__ help(sleep)
本文出自 “技术小菜” 博客,谢绝转载!
标签:python
原文地址:http://390892467.blog.51cto.com/2006821/1749624