标签:na
内置函数:
参考
https://docs.python.org/2/library/functions.html
装饰器
装饰器是函数,只不过该函数可以具有特殊的含义,装饰器用来装饰函数或类,使用装饰器可以在函数执行前和执行后添加相应操作
# 定义函数,为调用,函数内部不执行
# 函数名 > 代指函数
# @ + 函数名
# 功能:
# 1. 自动执行outer函数并且将其下面的函数名f1当作参数传递
# 2. 将outer函数的返回值,重复赋值给 f1
example 1:
#!/usr/bin/env python
# Author: Leon Wang Email: leonwang113@gmail.com
def outer(func):
def inner(*args,**kwargs):
print(‘before‘)
r = func(*args,**kwargs)
print("after")
return r
return inner
@outer
def f1(arg):
print(arg)
return"comeon"
@outer
def f2(arg1,arg2):
print("F2")
ret=f1("getdown")
print("返回值",ret)
f2(22,33)example 2:
#!/usr/bin/env python
# Author: Leon Wang Email: leonwang113@gmail.com
‘‘‘
def f1():
print(123)
def f2(a):
print(456)
f2(f1)
‘‘‘
‘‘‘
def login(func):
print("NB, passed user verification.....")
return func
def home(name):
print("Welcome [%s] to home page" % name)
@login
def tv(name):
print("Welcome [%s] to tv page" % name)
def movie(name):
print("Welcome [%s] to movie page" % name)
#tv = login(tv)
tv("Leon")
‘‘‘
def login(func):
def inner(arg):
print("NB, passed user verification.....")
func(arg)
return inner
def home(name):
print("Welcome [%s] to home page" % name)
@login
def tv(name):
print("Welcome [%s] to tv page" % name)
def movie(name):
print("Welcome [%s] to movie page" % name)
#tv = login(tv)
tv("Leon")本文出自 “风继续吹” 博客,请务必保留此出处http://fengjixuchui.blog.51cto.com/854545/1786026
标签:na
原文地址:http://fengjixuchui.blog.51cto.com/854545/1786026