码迷,mamicode.com
首页 > 编程语言 > 详细

python杂谈

时间:2019-11-15 12:27:19      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:exe   sdn   inpu   puts   item   int   net   ==   app   

1.装饰器

不更改原来函数的内容,给原函数增加额外功能。

def myDecorator(func):
def wraper(*args,**kwargs):# *args,**kwargs 兼容各种参数,也可以无参。

start_time = time.time()
f= func(*args,**kwargs)
end_time = time.time()
execution_time = (end_time - start_time) * 1000
print("time is %d ms" % execution_time)
return f #兼容带有返回值的方法
return wraper

@myDecorator
def sourceMethod(t,k=‘32‘):
time.sleep(t)
print(‘call back mq‘)
if __name__ == ‘__main__‘:
sourceMethod(2,k=‘fd‘)

2. yield 产生生成器或生成函数。
https://blog.csdn.net/mieleizhi0522/article/details/82142856

3.Map

map(function_to_apply, list_of_inputs)
大多数时候,我们要把列表中所有元素一个个地传递给一个函数,并收集输出
items = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, items))

 

python杂谈

标签:exe   sdn   inpu   puts   item   int   net   ==   app   

原文地址:https://www.cnblogs.com/peak911/p/11865361.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!