day12
4.函数名的多种用法
函数名其实绑定的也是一块内存地址 只不过该地址里面存放的不是数据值而是一段代码 函数名加括号就会找到该代码并执行
1.可以当做变量名赋值
def index():pass
res = index
res()
2.可以当做函数的参数
def index():
print('from index')
def func(a):
print(a)
a()
func(index)
3.可以当做函数的返回值
def index():
print('from index')
def func():
print('from func')
return index
res = func()
print(res)
res()
def index():
print('from index')
def func():
print('from func')
return func
res = index()
print(res)
res()
4.可以当做容器类型(可以存放多个数据的数据类型)的数据
def register():
print('注册功能')
def login():
print('登录功能')
def withdraw():
print('提现功能')
def transfer():
print('转账功能')
def shopping():
print('购物功能')
# 定义功能编号与功能的对应关系
func_dict = {
'1': register,
'2': login,
'3': withdraw,
'4': transfer,
'5': shopping
}
while True:
print("""
1.注册功能
2.登录功能
3.提现功能
4.转账功能
5.购物功能
""")
choice = input('>>>:').strip()
if choice in func_dict:
func_name = func_dict.get(choice)
func_name()
else:
print('功能编号不存在')
# if choice == '1':
# register()
# elif choice == '2':
# login()
# elif choice == '3':
# withdraw()
# elif choice == '4':
# transfer()
# elif choice == '5':
# shopping()
# else:
# print('去你妹的 终于写完了')
5.闭包函数
6.函数传参的两种方式
# 了解
# 为函数体传值的方式
# 方式一:通过参数的形式为函数体传值
import requests
def get(url):
response = requests.get(url)
if response.status_code == 200:
print(response.text)
get('https://www.baidu.com')
# 方式二:通过闭包的方式为函数体传值
import requests
import time
def outter(url): # url='https://www.baidu.com'
# 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的
python = outter('https://www.python.org')
baidu()
python()
7.装饰器简介
什么是装饰器?
"""
在不改变被装饰对象原代码和调用方式的情况下给被装饰对象添加新的功能
"""
8.无参装饰器简单实现
# 统计函数运行时间代码
import time
def index():
start_time=time.time()
time.sleep(3)
print('welcome to index page')
stop_time=time.time()
print('run time is %s' %(stop_time-start_time))
index()
修订一:
import time
def index():
time.sleep(3)
print('welcome to index page')
def home(name):
time.sleep(5)
print('welcome %s to home page' %name)
start_time=time.time()
index()
stop_time = time.time()
print('run time is %s' % (stop_time - start_time))
start_time=time.time()
home('egon')
stop_time = time.time()
print('run time is %s' % (stop_time - start_time))
修订二:(为了将其封装成一个工具方便调用)
import time
def index():
time.sleep(3)
print('welcome to index page')
def home(name):
time.sleep(5)
print('welcome %s to home page' %name)
def wrapper(func): #func=index
start_time=time.time()
func() #index()
stop_time = time.time()
print('run time is %s' % (stop_time - start_time))
wrapper(index) # 修改了原函数的调用方式
修订三:(因为调用方式改了)
import time
def index():
time.sleep(3)
print('welcome to index page')
def outter(func): # func=最原始的index
# func=最原始的index
def wrapper():
start_time = time.time()
func()
stop_time = time.time()
print(stop_time - start_time)
return wrapper
index = outter(index) # 新的index=wrapper
index() # wrapper()
9.无参装饰器升级版
# 无参装饰器完整版
import time
def index():
time.sleep(1)
print('welcome to index page')
return 122
def home(name):
time.sleep(3)
print('welcome %s to home page'%name)
def timmer(func):
def wrapper(*args,**kwargs):
start_time=time.time()
res=func(*args,**kwargs)
stop_time=time.time()
print(stop_time-start_time)
return res
return wrapper
index=timmer(index)
home=timmer(home)
home(name='egon')
index()
#无参装饰器模板
def outter(func):
def inner(*args,**kwargs):
res=func(*args,**kwargs)
return res
return inner
10.装饰器模版(重要)
# 务必掌握
def outer(func):
def inner(*args, **kwargs):
# 执行被装饰对象之前可以做的额外操作
res = func(*args, **kwargs)
# 执行被装饰对象之后可以做的额外操作
return res
return inner
11.装饰器语法糖
import time
def timmer(func):
def wrapper(*args,**kwargs):
start_time=time.time()
res=func(*args,**kwargs)
stop_time=time.time()
print(stop_time-start_time)
return res
return wrapper
@timmer #index=timmer(index)
def index():
time.sleep(1)
print('welcome to index page')
return 122
@timmer # home=timmer(home)
def home(name):
time.sleep(2)
print('welcome %s to home page' %name)
index()
home('egon')
标签:index,day12,res,func,time,print,def
From: https://www.cnblogs.com/wangmiaolu/p/17909621.html