python局部变量高级函数匿名函数嵌套函数装饰器

Posted wherewhenwho

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python局部变量高级函数匿名函数嵌套函数装饰器相关的知识,希望对你有一定的参考价值。

1.局部变量

在函数内部,可以用Global,显式的声明为全局变量。这种方式永远不要这么用。

Ctrl+?多行注释i

2.高级函数

把函数名当做参数,传给函数

def add(a,b,f):

return f(a)+f(b)

res = add(3,-6,abs)

abs是内置函数

 

def bar():

print("in the bar")

def test1(func):

 

首先看第一个例子:
def bar():

print("in the bar")

def test1(func):

print(func)

func()

test1(bar)

<function bar at 0x0000019A43BC2E18>
in the bar

 其次看第二个例子:

def bar():
time.sleep(3)
print("in the bar")

def test1(func):
start_time = time.time()
func()
stop_time = time.time()
print("the func run time is: %s" %(stop_time-start_time))

test1(bar)

in the bar
the func run time is: 3.000192880630493

可以看出不修改bar()的源代码下,为其增加功能

最后看第三个例子:

  

def bar():
print("in the bar")

def test1(func):
print(func)
return func

print(test1(bar))
test1(bar)()

bar = test1(bar)
bar()

<function bar at 0x000002347ACE2E18>
<function bar at 0x000002347ACE2E18>
<function bar at 0x000002347ACE2E18>
in the bar
<function bar at 0x000002347ACE2E18>
in the bar

 

2.匿名函数:

lambda x:x*3

calc = lambda x:x*3

calc(3)

3.引用计数

python通过引用计数,回收内存

4.嵌套函数

在一个函数体内def另外一个函数

def foo():
print("in the foo")
def bar():
print("int the bar")
def love():
print("int the love")
love()
bar()
foo()

5.装饰器本质是函数,装饰其它函数,为其他函数添加附加功能。语法糖。

1.不能修改被装饰的函数的源代码

2.不能修改被装饰的函数的调用方式

成为透明性

实现:

1函数即“变量”,先定义再使用

2.高级函数

3.嵌套函数

import time
def timer(func):
def deco():
start_time = time.time()
func()
stop_time = time.time()
print("the func run time is: %s" % (stop_time - start_time))

return deco

def test1():
time.sleep(3)
print("in teh tes1")

def test2():
time.sleep(3)
print("in teh tes2")

test1=timer(test1)
test1()

in teh tes1
the func run time is: 3.0001118183135986

最终为:

import time
def timer(func):
def deco():
start_time = time.time()
func()
stop_time = time.time()
print("the func run time is: %s" % (stop_time - start_time))

return deco
@timer #test1=timer(test1)
def test1():
time.sleep(3)
print("in teh tes1")
@timer
def test2():
time.sleep(3)
print("in teh tes2")

test1()

最最终为:import time
def timer(func):
def deco(*args,**kvargs):
start_time = time.time()
func(*args,**kvargs)
stop_time = time.time()
print("the func run time is: %s" % (stop_time - start_time))

return deco
@timer
def test1():
time.sleep(3)
print("in the test1")
@timer
def test2(name,age):
time.sleep(3)
print("in the test",name,age)

test1()
test2("roger",22)

补充:

username,password = "roger","abc123"
def auth(func):
def wrapper(*args,**kvargs):
username = input("Username:")
password = input("Password:")
if username == "roger" and password =="abc123":
print("User has passed authenticated")
func(*args,**kvargs)
else:
print("invalid username or password")
exit()
return wrapper
def index():
print("welcome to index page")

@auth
def home():
print("welcome to home page")
@auth
def bbs():
print("welcome to bbs page")

index()
home()
bbs()

继续:
username,password = "roger","abc123"
def auth(func):
def wrapper(*args,**kvargs):
username = input("Username:")
password = input("Password:")
if username == "roger" and password =="abc123":
print("User has passed authenticated")
resu = func(*args,**kvargs)
print("after authentication")
return resu
else:
print("invalid username or password")
exit()
return wrapper
def index():
print("welcome to index page")

@auth
def home():
print("welcome to home page")
return "from home"
@auth
def bbs():
print("welcome to bbs page")

index()
print(home())
bbs()

再继续:
username,password = "roger","abc123"
def auth(auth_type):
print("auth function args",auth_type)
def out_wrapper(func):
def wrapper(*args, **kvargs):
print("wrapper function args:", *args,*kvargs)
if auth_type == "local":
username = input("Username:")
password = input("Password:")
if username == "roger" and password == "abc123":
print("User has passed authenticated")
resu = func(*args, **kvargs)
print("after authentication")
return resu
else:
print("invalid username or password")
exit()
elif auth_type == "ldap":
print("执行ldap的验证")
return wrapper
return out_wrapper
def index():
print("welcome to index page")

@auth(auth_type="local")
def home():
print("welcome to home page")
return "from home"
@auth(auth_type="ldap")
def bbs():
print("welcome to bbs page")

index()
print(home())
bbs()


 
















































































































































































































以上是关于python局部变量高级函数匿名函数嵌套函数装饰器的主要内容,如果未能解决你的问题,请参考以下文章

Python学习之路day4-函数高级特性装饰器

python高级之装饰器

python基础装饰器

python--递归函数匿名函数嵌套函数高阶函数装饰器生成器迭代器

Python函数编程——闭包和装饰器

Python装饰器