码迷,mamicode.com
首页 > 编程语言 > 详细

python 高阶函数与装饰器

时间:2016-12-07 23:06:24      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:原则   调用   嵌套   密码   其他   time   login   pytho   opp   

 

 

高阶函数定义
1.函数接收的参数是一个函数名
2.函数的返回值是一个函数名
以上两者满足任意一个,就是高阶函数

装饰器定义
本质就是函数,功能是为其他函数添加新功能
装饰器的原则

   1.不修改被装饰函数的源代码(开放封闭原则)

   2.为被装饰函数添加新功能后,不修改被修饰函数的调用方式

 

   装饰器=高阶函数+函数嵌套+闭包

   

# 无返回值无参数
import time

def timer(func):     #func = test
    def w():
        start_time = time.time()
        func()     #就是在运行test()
        stop_time = time.time()
        print("运行时间是%d"%(stop_time-start_time))
    return w

@timer    # 相当于 test = timer(test)
def test():
    time.sleep(2)
    print("from test")

# test = timer(test)  #返回的是w的地址
# test()       #相当于执行w

test()

 

#加上返回值
import time

def timer(func):       #func = test
    def w():
        start_time = time.time()
        res = func()    #就是在运行test()
        stop_time = time.time()
        print("运行时间是%d"%(stop_time-start_time))
        return res
    return w

@timer    # 相当于 test = timer(test)
def test():
    time.sleep(2)
    print("from test")
    return "这是test的返回值"

# test = timer(test)  #返回的是w的地址
# test()       #相当于执行w

res = test()
print(res)

 

#加上参数和返回值 装饰器最终形式
import time

def timer(func):          #func = test   #func = test1
    def w(*args,**kwargs):
        start_time = time.time()
        res = func(*args,**kwargs)     #就是在运行test()   test1()
        stop_time = time.time()
        print("运行时间是%d"%(stop_time-start_time))
        return res
    return w


@timer    # 相当于 test = timer(test)
def test(name,age):
    time.sleep(2)
    print("from test   %s   %s"%(name,age))
    return "这是test的返回值"


res = test("liao",18)
print(res)

@timer     # 相当于 test1 = timer(test1)
def test1(name,age,g):
    time.sleep(1)
    print("from test1   %s   %s  %s"%(name,age,g))
    return "这是test1的返回值"

res1 = test1("bo",26,"shi")
print(res1)

 

用户登陆(简单流程判断)

l = [{"name":"liao","pwd":"123"},{"name":"tom","pwd":"123"}]     #用户数据
c_d = {"user":None,"login":False}          #定义一个空的临时的用户字字典

def a_f(func):
    def w(*args,**kwargs):
        if c_d["user"] and c_d["login"]:      #判断临时字典里是否有用户登陆,没有就输入
            res = func(*args,**kwargs)        #有就进入下一步
            return res

        user = input("请输入用户名:").strip()   #临时字典没有数据就输入用户名
        pwd = input("请输入密码:").strip()      #临时字典没有数据就输入密码

        for i in l:          #遍历用户数据
            if user == i["name"] and pwd == i["pwd"]:    #判断输入的用户和密码是否在用户数据里
                c_d["user"] = user        #输入正确,数据保存到临时的用户字字典里,下一步不用再输入用户和密码
                c_d["login"] = True
                res = func(*args,**kwargs)    #进入
                return res
        else:                       #如果输入的用户名和密码不在用记数据里,提示用户
            print("用户名或者密码错误")
    return w

@a_f
def index():
    print("欢迎来到主页面")

@a_f
def home():
    print("这里是你家")

@a_f
def shopping_car():
    print("查看购物车啊亲")

index()
home()
shopping_car()

 

 

python 高阶函数与装饰器

标签:原则   调用   嵌套   密码   其他   time   login   pytho   opp   

原文地址:http://www.cnblogs.com/liaoboshi/p/6142957.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!