装饰器
import time import random # def timmer(func): # def wrapper(): # start_time = time.time() # func() # stop_time = time.time() # return wrapper # # def index(): # time.sleep(random.randrange(1,3)) # print('实现功能') # # def index(): # time.sleep(random.randrange(1, 3)) # print('实现功能i') # # # def home(): # time.sleep(random.randrange(1, 3)) # print('实现功能h') # # # index = timmer(index) # index = timmer(home) def timmer(func): def wrapper(): start_time = time.time() func() stop_time = time.time() return wrapper @timmer # index = timmer(index) 装饰器?? 相当于把下列函数的函数名作为实参传递给timmer函数的形参 def index(): time.sleep(random.randrange(1,3)) print('实现功能i') @timmer #index = timmer(home) def home(): time.sleep(random.randrange(1,3)) print('实现功能h') index() home()
装饰器:
# 一:编写函数,(函数执行的时间是随机的) # import time # import random # # @timmer # def func1(): # time.sleep(random.randrange(1, 3)) # print('执行函数成功') # 二:编写装饰器,为函数加上统计时间的功能 import time import random def timmer(func): def wrapper(): start_time = time.time() func() stop_time = time.time() print('函数执行时间 %d' % (stop_time-start_time)) return wrapper @timmer def func1(): time.sleep(random.randrange(1, 3)) print('执行函数成功') func1() # 三:编写装饰器,为函数加上认证的功能 import time import random def auth(func): def wrapper(): username = input('Please input your name: ') password = input('Please input your password') if username == 'cx2c' and password == 'cx2c': func() else: print('Wrong username or password') return wrapper @auth def func2(): time.sleep(random.randrange(1, 3)) print('Congratulations to process successfully') func2() 四:编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件),要求登录成功一次,后续的函数都无需再输入用户名和密码 注意:从文件中读出字符串形式的字典,可以用eval('{"name":"egon","password":"123"}')转成字典格式 装饰器: 为多个函数加上认证功能 用户名 密码来源于文件 认证一次,后面都可以执行 tag = False def auth(func): def login(): global tag if tag: func() else: x = [] with open('user-password.txt', 'r') as f: username = input('user: ') password = input('pass: ') for i in f: x.append(eval(i)) for dict_login in x: if dict_login['username'] == username and dict_login['password'] == password: func() tag = True else: continue return login @auth def shooping_cart(): print('shopping successful') @auth def buy_vip(): print('buy vip successful') @auth def book_tickets(): print('booking ticket successfully') shooping_cart() buy_vip() book_tickets() def auth(func): def login(): x = [] with open('user-password.txt', 'r') as f: username = input('user: ') password = input('pass: ') for i in f: x.append(eval(i)) for dict_login in x: if dict_login['username'] == username and dict_login['password'] == password: func() return login @auth def shooping_cart(): print('shopping successful') @auth def buy_vip(): print('buy vip successful') @auth def book_tickets(): print('booking ticket successfully') shooping_cart() buy_vip() book_tickets() # 五:编写下载网页内容的函数,要求功能是:用户传入一个url,函数返回下载页面的结果 from urllib.request import urlopen # print(urlopen('http://www.baidu.com').read()) def index(url): print(type(url)) def get(): print(urlopen(url).read()) return get url_input = input('Please input your url') index(url_input)() # 六:为题目五编写装饰器,实现缓存网页内容的功能: # 具体:实现下载的页面存放于文件中,如果文件内有值(文件大小不为0),就优先从文件中读取网页内容,否则,就去下载,然后存到文件中 from urllib.request import urlopen def cache_index(fun): def wrapper(): with open('index.html', 'r', encoding='utf-8') as f: if len(f.read()) == 0: # with open('index.html', 'w', encoding='utf-8') as ff: ff.write(str(fun())) return wrapper @cache_index def get_index(): url_input = input('Please inout your url') dd = urlopen(url_input).read() return dd get_index() # 七:还记得我们用函数对象的概念,制作一个函数字典的操作吗,来来来,我们有更高大上的做法,在文件开头声明一个空字典,然后在每个函数前加上装饰器,完成自动添加到字典的操作 dict_1 = {} def add_dict(fun): def wrapper(): dict_1[fun.__name__] = fun print(dict_1) return wrapper @add_dict def shooping_cart(): print('shopping successful') print(shooping_cart) @add_dict def buy_vip(): print('buy vip successful') @add_dict def book_tickets(): print('booking ticket successfully') shooping_cart() buy_vip() book_tickets()
闭包函数
# 闭包函数: 闭 + 包 包了一层作用域关系 :变量不是局部就不是闭包函数 # 闭包函数: 自带作用域;延迟计算 closures # 函数的作用域关系,在函数定义阶段已经定义 def func1(): name = 'alex' def func2(): print(name) return func2 aa = func1() print(aa) aa() # __closure__ 关闭 用法? money = 1000 def func1(): x = 100 def func2(): print(x) return func2 f = func1() print(f) f() def first(): name = 'egon' def second(): money = 1000 print(egon,money) return second return first