Python基础第11天

一:生成器补充:

     生成器只能遍历一次

#生成器只能遍历一次
def test():
    for i in range(4):
        yield i

t=test()  #产生生成器
for i in t:
    print(i)
t1=(i for i in t)  #t已经遍历完了所以输出为空
print(list(t1))

#[0, 1, 2, 3]
#[]
t1=(i for i in t) #获取内存地址
t2=(i for i in t1)
print(list(t1)) #遍历t1
print(list(t2))
#[0, 1, 2, 3]
#[]

二:装饰器:

本质是函数  功能是为其他函数添加新功能
原则:不修改被修饰函数的源代码;不修改被修饰函数的调用方式
实现装饰器知识储备:装饰器=高阶函数+函数嵌套+闭包
高阶函数:
#1.函数接收的参数是一个函数名,实现了加功能,不能保证不改变调用方式
import time
def foo():
    print('你好啊')
def test(func):
    print(func) #foo内存地址
    start_time=time.time()
    func()
    stop_time = time.time()
    print('函数运行时间是 %s'%(stop_time-start_time))
test(foo)
#2.函数的返回值是一个函数名
def foo():
    print('from the foo')
def test(func):
    return func
res=test(foo) #foo内存地址
res()  #运行foo
foo=test(foo)
foo()#不改变调用方式
#结合以上
import time
def foo():
    time.sleep(3)
    print('来至foo')
#不修改foo源代码
#不修改foo调用方式

def timer(func):
    start_time = time.time()
    func()
    stop_time = time.time()
    print('函数运行时间是 %s'%(stop_time-start_time)
    return func
foo=timer(foo)
foo()

函数嵌套   函数里面又定义另外函数——闭包:函数作用域


def father(name):
    print('from father %s' %name)
    def son():
        print('from son')
        def grandson():
            print('from grandson')
        grandson()
    son()

father('林海峰)

#装饰器框架
def timer(func):
    def wrapper():
        print(func)
        func()
    return wrapper

#加上返回值
import time
def timer(func):  #func=test
    def wrapper():
        start_time=time.time()
        res=func()   #就是运行test()
        stop_time = time.time()
        print('函数运行时间是 %s' % (stop_time - start_time))
        return  res
    return wrapper

@timer  #test=timer(test)
def test():
    time.sleep(3)
    print('test函数运行完毕')
    return '这是test返回值'

res=test()  #运行wrapper
print(res)

#加参数
import time
def timer(func):  #func=test
    def wrapper(*args,**kwargs):
        start_time=time.time()
        res=func(*args,**kwargs)   #就是运行test()
        stop_time = time.time()
        print('函数运行时间是 %s' % (stop_time - start_time))
        return  res
    return wrapper

@timer  #test=timer(test)
def test(name,age):
    time.sleep(3)
    print('test函数运行完毕,名字是【%s】' %(name,age))
    return '这是test返回值'

@timer  #test=timer(test)
def test1(name,age,gender):
    time.sleep(1)
    print('test函数运行完毕,名字是【%s】性别 [%s]' %(name,age,gender))
    return '这是test返回值'

res=test('linhaifeng',age=18)  #运行wrapper
print(res)
test1('alex',18,'male')

#函数闭包为函数加上认证功能
def auth_func(func):
    def wrapper(*args,**kwargs):
        username=input('用户名:').strip()
        passwd=input('密码:').strip()
        if username == 'sb' and passwd == '123':
            res=func(*args,**kwargs)
        else:
            print('用户名或密码错误')
        return res
    return wrapper

@auth_func
def index():
    print('欢迎来到主页面')

@auth_func
def home(name):
    print('欢迎回家%s' %name)

@auth_func
def shopping_car(name):
    print('%s的购物车里有【%s,%s,%s】' %(name,'商品一','商品二','商品三'))

index()
home('经理')
shopping_car('经理')

#更加合理
user_list=[
    {'name':'alex','passwd':'123'},
    {'name':'linhaifeng','passwd':'123'},
    {'name':'wupeiqi','passwd':'123'},
    {'name':'yuanhao','passwd':'123'},
]

current_dic={'username':None,'login':False}#全局变量
def auth(auth_type='filedb'):
    def auth_func(func):
        def wrapper(*args,**kwargs):
            print('认证类型',auth_type)
            if auth_type == 'filedb':
                if current_dic['username'] and current_dic['login']:
                    res = func(*args, **kwargs)
                    return res
                username = input('用户名:').strip()
                passwd = input('密码:').strip()
                for user_dic in user_list:
                    if username==user_dic['name'] and passwd == user_dic['passwd']:
                        current_dic['username']=username
                        current_dic['login'] = True
                        res = func(*args, **kwargs)
                        return res
                else:
                    print('用户名或密码错误')

        return wrapper
    return auth_func

@auth(auth_type='filedb')
def index():
    print('欢迎来到主页面')

# @auth_func
def home(name):
    print('欢迎回家%s' %name)

print('before-->',current_dic)
index()
print('after-->',current_dic)
home('经理')
 
原文地址:https://www.cnblogs.com/xyd134/p/6486613.html