x=1000
def func(y):
x=2
print(locals())
print(globals())
func(1)
输出:
{'y': 1, 'x': 2}
{'__loader__': <_frozen_importlib_external.sourcefileloader object at>, '__package__': None, '__cached__': None, '__file__': '/Users/hexin/PycharmProjects/py3/day4/2.py', 'func': , '__builtins__': , '__spec__': None, '__doc__': None, 'time': , '__name__': '__main__', 'x': 1000}
四.闭包函数
简单来说,一个闭包就是你调用了一个函数A,这个函数A返回了一个函数B给你。这个返回的函数B就叫做闭包。
闭包函数须满足以下条件:
1. 定义在内部函数;
2. 包含对外部作用域而非全局作用域的引用;
def f1():
x = 1
def f2():
print(x)
return f2
f=f1()
print(f)
x=100
f()
print(x)
输出:
.f2 at 0x107714400>
1
100
闭包的应用
from urllib.request import urlopen
def index(url):
def get():
return urlopen(url).read()
return get
oldboy=index('http://crm.oldboyedu.com')
print(oldboy().decode('utf-8'))
五。装饰器
1.定义
装饰器:修饰别人的工具,修饰添加功能,工具指的是函数
装饰器本身可以是任何可调用对象,被装饰的对象也可以是任意可调用对象
2.为什么要用装饰器?
开放封闭原则:对修改是封闭的,对扩展是开放的
装饰器就是为了在不修改被装饰对象的源代码以及调用方式的前提下,为其添加新功能
3.装饰器的实现
装饰器的功能是将被装饰的函数当作参数传递给与装饰器对应的函数(名称相同的函数),并返回包装后的被装饰的函数”
直接看示意图,其中 a 为与装饰器 @a 对应的函数, b 为装饰器修饰的函数,装饰器@a的作用是:
简而言之:@a 就是将 b 传递给 a(),并返回新的 b = a(b)
def a(name): #与装饰器对应的函数
return name()
@a #装饰器 b = a(b)
def b(): #被装饰函数
print('hexin')
输出
hexin
解析过程是这样子的:
1.python 解释器发现@a,就去调用与其对应的函数( a 函数)
2.a 函数调用前要指定一个参数,传入的就是@a下面修饰的函数,也就是 b()
3.a() 函数执行,调用 b(),b() 打印“hexin”
装饰器的应用
importtimedeftimmer(func):defwrapper():
start_time=time.time()
func()#index()
stop_time=time.time()print('run time is %s' %(stop_time-start_time))returnwrapper
@timmer#index=timmer(index)
defindex():
time.sleep(1)print('welcome to index')
index()
输出:
welcome to index
run timeis 1.005241870880127
#--------------------------------------------------------------
login_user={'user':None,'status':False}defauth(func):def wrapper(*args,**kwargs):if login_user['user'] and login_user['status']:
res=func(*args,**kwargs)returnreselse:
name=input('请输入用户名:')
password=input('请输入密码:')if name == 'hexin' and password == '123':
login_user['user']='hexin'login_user['status']=Trueprint('\033[45mlogin successful\033[0m')
res=func(*args,**kwargs)returnreselse:print('\033[45mlogin err\033[0m')returnwrapper
@auth#index=auth(index)
defindex():print('welcome to index page')
@auth#home=auth(home)
defhome(name):print('%s welcome to home page' %name)
index()
home('hexin')
输出:
请输入用户名: heixn
请输入密码:123login err
请输入用户名: hexin
请输入密码:123login successful
hexin welcome to home page
装饰器的基本框架:
def timer(func):
def wrapper():
func()
return wrapper
带参数
def timer(func):
def wrapper(*args,**kwargs):
func(*args,**kwargs)
return wrapper