python杂谈

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))

 3.相等比较

 is与==的区别就是,is是内存中的比较,而==是值的比较

详见 https://www.cnblogs.com/wangkun122/p/9082088.html

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