Python 函数(三)

Python 3 函数 (闭包、装饰器、递归、高阶函数)

一、闭包

内部函数可以引用外部函数的参数和局部变量,当外部函数返回内部函数时,相关参数和变量

都保存在返回的函数中,简单的说,这种内部函数可以使用外部函数变量的行为,就叫闭包。

例一:

def sum_out(*args):

    def sum_in():   #定义内内部函数

        sumV = sum(args)    #使用外部函数的args变量

        return sumV

    return sum_in   #返回内部函数

S = sum_out(1, 2, 3)

print(S()) #真正调用的是sum_in函数

Python 3 结果:6

例二:

def add1():

    a = 1

    b = 2

    def add2(m, n):

        return a + b + m + n

    return add2

Add = add1()

print(Add(5,6))

Python 3 结果:14

二、装饰器

装饰器其实就是一个以函数作为参数并返回一个替换函数的可执行函数;

简言之,python装饰器就是用于拓展原来函数功能的一种函数,这个函数的特殊之处在于它的返回值也是一个函数,

使用python装饰器的好处就是在不用更改原函数的代码前提下给函数增加新的功能。

基本格式(记录下函数执行的时间):

import time

def add():

    print('add')

add()

def log(func):

    def wrapper():

        print('调用%s函数时间:%s' % (func.__name__, time.strftime('%Y-%m-%d %H:%M:%S')))

        func()

    return wrapper

wrap1 = log(add)

wrap1()

 注:__name__可以获得函数名称

例一(带有参数的装饰器):

import time

def deco(func):

    def wrapper(a,b):

        startTime = time.time()

        func(a,b)

        endTime = time.time()

        msecs = (endTime - startTime)*1000

        print("time is %d ms" %msecs)

    return wrapper

@deco

def func(a,b):

    print("hellohere is a func for add :")

    time.sleep(1)

    print("result is %d" %(a+b))

if __name__ == '__main__':

    f = func

    f(3,4)

例二(带有不定参数的装饰器):

import time

def deco(func):

    def wrapper(*args, **kwargs):

        startTime = time.time()

        func(*args, **kwargs)

        endTime = time.time()

        msecs = (endTime - startTime)*1000

        print("time is %d ms" %msecs)

    return wrapper

@deco

def func(a,b):

    print("hellohere is a func for add :")

    time.sleep(1)

    print("result is %d" %(a+b))

@deco

def func2(a,b,c):

    print("hellohere is a func for add :")

    time.sleep(1)

    print("result is %d" %(a+b+c))

if __name__ == '__main__':

    f = func

    func2(3,4,5)

    f(3,4)

例三(多个装饰器):

import time

def deco01(func):

    def wrapper(*args, **kwargs):

        print("this is deco01")

        startTime = time.time()

        func(*args, **kwargs)

        endTime = time.time()

        msecs = (endTime - startTime)*1000

        print("time is %d ms" %msecs)

        print("deco01 end here")

    return wrapper

def deco02(func):

    def wrapper(*args, **kwargs):

        print("this is deco02")

        func(*args, **kwargs)

        print("deco02 end here")

    return wrapper

@deco01

@deco02

def func(a,b):

    print("hellohere is a func for add :")

    time.sleep(1)

    print("result is %d" %(a+b))

if __name__ == '__main__':

    f = func

    f(3,4)

 

    

欢迎关注小婷儿的博客:https://blog.csdn.net/u010986753

有问题请在博客下留言或加QQ群:483766429 或联系作者本人 QQ 87605025


OCP培训说明连接:https://mp.weixin.qq.com/s/2cymJ4xiBPtTaHu16HkiuA

OCM培训说明连接:https://mp.weixin.qq.com/s/7-R6Cz8RcJKduVv6YlAxJA

 

小婷儿的python正在成长中,其中还有很多不足之处,随着学习和工作的深入,会对以往的博客内容逐步改进和完善哒。

小婷儿的python正在成长中,其中还有很多不足之处,随着学习和工作的深入,会对以往的博客内容逐步改进和完善哒。

小婷儿的python正在成长中,其中还有很多不足之处,随着学习和工作的深入,会对以往的博客内容逐步改进和完善哒。

重要的事说三遍。。。。。。

 

 

posted @ 2018-04-21 13:21  小婷儿  阅读(347)  评论(0编辑  收藏  举报
levels of contents