闭包函数和装饰器

     闭包函数

#作用域关系在函数定义阶段时就已经固定死了,与调用位置无关
# 即:在任意位置调用函数都需要跑到定义函数时寻找作用域关系

# def f1():
# x=1
# def inner():
# print(x) #x最后还是等于1,因为只看定义阶段,
#
# return inner
#
# func=f1()
#
# def f2():
# x=111111
# func()
#
# f2()

# 闭包函数:
# 闭指的是:该函数是一个内部函数
# 包指的是:指的是该函数包含对外部作用域(非全局作用域)名字的引用
# def outter():
# x = 1
# def inner():
# print(x)
#
# return inner #outter()拿到的是inner的地址。
#
# f=outter()
#
# def f2():
# x=1111111
# f()
#
# f2()
#
#
# def f3():
# x=4444444444444
# f()
#
# f3()

# 注意:当函数体需要传值的时候,有两种方法:

# 为函数体传值的方式一:使用参数的形式
# def inner(x):
# print(x)
#
# inner(1)
# inner(1)
# inner(1)
# 为函数体传值的方式二:包给函数
'''
def outter(x):
# x=1 #相当于把x=1包给了inner
def inner():
print(x)
return inner

f=outter(1) #f=inner
f()
'''

# 闭包函数的应用场景一:再爬虫下载网站的时候:
# 第一种用传参的方式:
# import requests
#
# def get(url):
# response=requests.get(url)
# if response.status_code == 200:
# print(response.text)
#
# get('https://www.baidu.com')
# get('https://www.baidu.com')
# get('https://www.baidu.com')
#
# get('https://www.python.org')
# get('https://www.python.org')
# get('https://www.python.org')
# get('https://www.python.org')


第二种用闭包函数的方式:
import requests

def outter(url):
# url='https://www.baidu.com'
def get():
response=requests.get(url)
if response.status_code == 200:
print(response.text)
return get

baidu=outter('https://www.baidu.com')
python=outter('https://www.python.org')


baidu()
baidu()

python()
python()



装饰器


1、什么是装饰器
器指的是工具,而程序中的函数就具备某一功能的工具
装饰指的是为被装饰器对象添加额外功能

就目前的知识来看:
定义装饰器就是定义一个函数,只不过该函数的功能是用来为
其他函数添加额外的功能

其实:
装饰器本身其实可以是任意可调用的对象
被装饰的对象也可以是任意可调用的对象


2、为什么要用装饰器
软件的维护应该遵循开放封闭原则
开放封闭原则指的是:
软件一旦上线运行后对修改源代码是封闭的,对扩展功能的是开放的
这就用到了装饰器

装饰器的实现必须遵循两大原则:
1、不修改被装饰对象的源代码
2、不修改被装饰对象的调用方式
装饰器其实就在遵循1和2原则的前提下为被装饰对象添加新功能

3、如何用装饰器
'''

# import time
#
# def index():
# start=time.time()
# print('welcom to index')
# time.sleep(3)
# stop=time.time()
# print('run time is %s' %(stop-start))
# index() #这种方式改变了原代码

# import time
#
# def index():
# print('welcom to index')
# time.sleep(3)
#
# def f2():
# print('from f2')
# time.sleep(2)
#
# start=time.time()
# index()
# stop=time.time()
# print('run time is %s' %(stop-start))
#
# start=time.time()
# f2()
# stop=time.time()
# print('run time is %s' %(stop-start))









# import time
#
# def index():
# print('welcom to index')
# time.sleep(3)
#
# def timmer(func):
# start=time.time()
# func()
# stop=time.time()
# print('run time is %s' %(stop-start))
#
# timmer(index)




'''
import time

def index():
print('welcom to index')
time.sleep(3) #被装饰函数的原代码

def timmer(func): #func=最原始的index
# func=index
def inner():
start=time.time()
func() #调用外层包给inner的值,
stop=time.time()
print('run time is %s' %(stop-start))
return inner

# f=timmer(index)
# f()

# index=timmer(被装饰函数的内存地址)
index=timmer(index) #index=inner

index() #inner()


'''
import time

def index():
print('welcom to index')
time.sleep(3)

def timmer(func):
#func=最原始的index
def wrapper():
start=time.time()
func()
stop=time.time()
print('run time is %s' %(stop - start))
return wrapper

index=timmer(index) #index=wrapper函数的内存地址
index()


装饰器修正


# import time
#
# def index():
# print('welcome to index')
# time.sleep(3)
# return 123
#
# def home(name):
# print('welcome %s to home page' %name)
# time.sleep(2)
#
#
# def timmer(func):
# #func=最原始的index
# def wrapper(*args,**kwargs):
# start=time.time()
# res=func(*args,**kwargs) #把func赋值给res,是为了让变化过的index和最原始的index返回值一样,其实就是多赋值了一个变量
# stop=time.time()
# print('run time is %s' %(stop - start))
# return res
# return wrapper
#
#
# index=timmer(index)
# home=timmer(home)
#
# res=index()
# home('egon')


 

猜你喜欢

转载自www.cnblogs.com/fxc-520520/p/9168833.html
今日推荐