python第八节(函数2)

函数2

内置函数

'''内置函数
map()
zip()'''
'''返回1-10的奇数列表'''
# for i in range(1,10,2):
#     print(i)
# li = []
# for i in range(1,10):
#     if i%2 ==1:
#         li.append(i)
#         print(i)
# print(li)
'''filter 过滤'''
def is_odd(n):
    return n % 2 == 1
print(list(filter(is_odd, range(1, 10))))
'''
阶乘-->累积 循环,递归
reduce()函数会对参数序列中元素进行累积'''
from functools import reduce
def multi(x, y):
    return x * y
print(reduce(multi, range(1, 7)))  # 6!  [1,2,3,4,5,6]

匿名函数及高阶函数

def func_name(x):
    return x*x
func_name(1)
'''匿名函数
匿名函数关键字 参数:表达式返回值
lambda x:x*x'''
f = lambda x:x*x
print(f(5))
from functools import reduce
# def multi(x, y):
#     return x * y
# print(reduce(multi, range(1, 7)))  # 6!  [1,2,3,4,5,6]
print(reduce(lambda x,y:x*y,range(1,7)))
'''
匿名函数可以直接作为函数返回值'''
def fx(i,j):
    return lambda :i*j
# print(fx(6,6))
res = fx(6,6)   # lambda :i*j = fx(6,6)
print(res())
'''
匿名函数当作参数'''
def test(a,b,func):
    res = func(a,b)
    # res = a + b
    return res
res = test(11,33,lambda x,y:x+y)
print(res)
'''
高阶函数
1.函数名可以作为参数输入
2.函数名可以作为返回值
只要满足条件之一-->高阶函数
map()
filter()
reduce()
'''

匿名函数高阶函数作业

'''不改变原有的符号,但是以绝对值来排序'''
lis = [4,-2,3,-1]
lis.sort(key=abs)
print(lis)
infor = [{'name':'qian','age':28},{'name':'amy','age':20},
         {'name':'Tisoy','age':25}]
infor.sort(key=lambda x:x['age'])
print(infor)

闭包装饰器

# def test():
#     print(11111)
# te = test()  # name = 1
# # print(id(te))
# # print(id(test()))
# te()
'''
闭包
1.在一个外函数中定义一个内函数
2.内函数里运用了外函数的临时变量
3.外函数的返回值是内函数的引用
'''
def test(number):
    print("---1---")
    def test_in(number_in):  # 内部函数
        print(number_in)
        print('---2---')
        return number + number_in  # 内部函数运用了外函数的临时变量number
    print('---3---')
    return test_in  # 外函数的返回值是内函数的引用
res = test(20)  # res = test_in
res(25)
in_res = res(25)  # in_res = number + number_in
print(in_res)   #  代码读写  从上至下
'''
实现计算函数运行时间功能
'''
import time
'''
想要添加验证功能?一定遵循封闭开放原则
'''
def outer(flag):
    def calcu_time(func):
        print('我在装饰噢')
        def test_in():
            start = time.time()
            func()
            end = time.time()
            print('spend {}'.format(end-start))
            if flag == 'true':
                print("验证成功")
        return test_in
    return calcu_time
@outer(flag='true')  # tess2 = calcu_time(test2)
def test2():
    print('---test2---')
    time.sleep(1.5)
# @calcu_time  # tess2 = calcu_time(test2)
# def test3():
#     print('---test3---')
#     time.sleep(2)
# @calcu_time()  # tess2 = calcu_time(test2)
# def test4():
#     print('---test4---')
#     time.sleep(1)
def test1():
    start = time.time()
    print('---test1---')
    time.sleep(2)
    end = time.time()
    print('spend {}'.format(end-start))
# test1()
# test2 = calcu_time(test2)  # test2 = test_in
test2()  # test_in()
# test3()
# test4()
'''
封闭:已经实现的代码块,则尽量可能不做任何修改
开放:允许扩展新的功能
@语法糖
@calcu_time 装饰器,在不改变源代码的情况下添加新的功能
'''
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值