python 定义函数 返回值只取其中一个的实现
作者:第二i 时间:2022-07-29 21:28:21
看代码吧~
def test():
return 1,2
a, b = test()
1 2
a, _ = test()
1
# 说明 a, _ = test() 中 返回的是一个int类型 后续可直接运算
# a, b = test() 返回值是一个元组, 直接运算报错
补充:Python-装饰器(被装饰函数有返回值的情况)
题目:
装饰器实现一个函数计时器比较for循环与map,匿名函数的运算速度
问题:
被装饰的函数有返回值怎么办
程序内容:
import time
import random
import string
import functools
#问题:被装饰的函数有返回值
li = [random.choice(string.ascii_letters) for i in range(100)]
def timeit(fun):
@functools.wraps(fun) #为了保留被装饰函数的函数名和帮助文档信息
def wrapper(*args,**kwargs):
"""这是一个wrapper函数"""
start_time = time.time()
res = fun(*args,**kwargs)
end_time = time.time()
print('运行时间为:%.6f' %(end_time - start_time))
return res
return wrapper
@timeit
def fun_list(n):
"""这是fun_list函数"""
return [i * 2 for i in range(n)]
@timeit
def fun_map(n):
"""这是fun_map函数"""
return list(map(lambda x:x*2,range(n)))
print(fun_list(5000))
print(fun_map(5000))
测试:
来源:https://blog.csdn.net/weixin_42785547/article/details/86361363
标签:python,函数,返回值
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使用IIS调试asp程序检查错误的方法
2007-09-13 21:54:00
Django 跨域请求处理的示例代码
2022-05-27 17:08:46
![](https://img.aspxhome.com/file/2023/3/65503_0s.png)
Oracle8i和Microsoft SQL Server比较
2010-07-26 13:05:00
用FrontPage200八步快速建站
2008-09-17 10:52:00
![](https://img.aspxhome.com/file/UploadPic/200810/17/20081017105646475.jpg)
php ajax异步读取rss文档数据
2023-10-17 19:59:02
Go 字符串格式化的实例代码详解
2023-08-05 14:05:30
Python文件操作类操作实例详解
2023-05-19 04:45:08
python下对hsv颜色空间进行量化操作
2022-11-22 01:56:12
![](https://img.aspxhome.com/file/2023/6/93966_0s.jpg)
CSS Shadow Practice
2009-04-01 18:37:00
python3.7简单的爬虫实例详解
2023-06-30 15:55:13
使用php-timeit估计php函数的执行时间
2023-10-07 19:56:50
python读取目录下所有的jpg文件,并显示第一张图片的示例
2023-06-21 16:31:58
用Python进行TCP网络编程的教程
2021-07-31 06:06:07
![](https://img.aspxhome.com/file/2023/3/89453_0s.png)
ThinkPHP中limit()使用方法详解
2023-11-14 22:05:04
在Ubuntu系统下安装使用Python的GUI工具wxPython
2022-08-21 10:17:49
如何将 Access 的 Memo 型态字段汇入到 SQL2005 的 nvarchar 型态字段
2008-12-26 18:13:00
Python开发之迭代器&生成器的实战案例分享
2021-03-18 17:34:33
用户体验的另一种认识
2007-10-25 12:36:00
![](https://img.aspxhome.com/file/UploadPic/200710/25/20071025124243541s.jpg)
Python实现随机森林回归与各自变量重要性分析与排序
2023-05-04 05:52:21
![](https://img.aspxhome.com/file/2023/0/69580_0s.png)
python 使用GDAL实现栅格tif转矢量shp的方式小结
2021-10-02 07:13:56
![](https://img.aspxhome.com/file/2023/9/88859_0s.jpg)