*args与**kwargs
在装饰器用的比较多
生成器Generators
生成器也是一种迭代器,但是只能对其迭代一次。因为它们并没有把所有的值存在内存中,而是在运行时生成值。
# 斐波那契数列 generator version
def fibon(n):
a = b = 1
for i in range(n):
yield a
a, b = b, a + b
三元运算符
#如果条件为真,返回真 否则返回假
condition_is_true if condition else condition_is_false
装饰器
装饰器是修改其他函数的功能的函数
在python中,一切皆对象,包括函数
将函数作为参数传给另一个函数
def hi():
return "hi yasoob!"
def doSomethingBeforeHi(func):
print("I am doing some boring work before executing hi()")
print(func())
doSomethingBeforeHi(hi)
#outputs:I am doing some boring work before executing hi()
# hi yasoob!
简化
@a_new_decorator
def a_function_requiring_decoration():
"""Hey you! Decorate me!"""
print("I am the function which needs some decoration to "
"remove my foul smell")
a_function_requiring_decoration()
函数名被修改了
print(a_function_requiring_decoration.__name__)
# Output: wrapTheFunction
使用@wraps修正这种情况
def a_new_decorator(a_func):
@wraps(a_func)
def wrapTheFunction():
print("I am doing some boring work before executing a_func()")
a_func()
print("I am doing some boring work after executing a_func()")
return wrapTheFunction
使用场景
- 授权(Authorization) (像spring的aop一样)
装饰器能有助于检查某个人是否被授权去使用一个web应用的端点(endpoint)。它们被大量使用于Flask和Django web框架中。这里是一个例子来使用基于装饰器的授权:
from functools import wraps
def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
if not auth or not check_auth(auth.username, auth.password):
authenticate()
return f(*args, **kwargs)
return decorated
- 日志(Logging)
from functools import wraps
def logit(func):
@wraps(func)
def with_logging(*args, **kwargs):
print(func.__name__ + " was called")
return func(*args, **kwargs)
return with_logging
@logit
def addition_func(x):
"""Do some math."""
return x + x
result = addition_func(4)
# Output: addition_func was called
- 在函数中嵌入装饰器
from functools import wraps
def logit(logfile='out.log'):
def logging_decorator(func):
@wraps(func)
def wrapped_function(*args, **kwargs):
log_string = func.__name__ + " was called"
print(log_string)
# 打开logfile,并写入内容
with open(logfile, 'a') as opened_file:
# 现在将日志打到指定的logfile
opened_file.write(log_string + '\n')
return func(*args, **kwargs)
return wrapped_function
return logging_decorator
@logit()
def myfunc1():
pass
myfunc1()
# Output: myfunc1 was called
# 现在一个叫做 out.log 的文件出现了,里面的内容就是上面的字符串
@logit(logfile='func2.log')
def myfunc2():
pass
myfunc2()
# Output: myfunc2 was called
# 现在一个叫做 func2.log 的文件出现了,里面的内容就是上面的字符串
- 装饰器类
from functools import wraps
class logit(object):
def __init__(self, logfile='out.log'):
self.logfile = logfile
def __call__(self, func):
@wraps(func)
def wrapped_function(*args, **kwargs):
log_string = func.__name__ + " was called"
print(log_string)
# 打开logfile并写入
with open(self.logfile, 'a') as opened_file:
# 现在将日志打到指定的文件
opened_file.write(log_string + '\n')
# 现在,发送一个通知
self.notify()
return func(*args, **kwargs)
return wrapped_function
def notify(self):
# logit只打日志,不做别的
pass
此时还可以
@logit()
def myfunc1():
pass
有了类,就可以面向对象了
class email_logit(logit):
'''
一个logit的实现版本,可以在函数调用时发送email给管理员
'''
def __init__(self, email='admin@myproject.com', *args, **kwargs):
self.email = email
super(email_logit, self).__init__(*args, **kwargs)
def notify(self):
# 发送一封email到self.email
# 这里就不做实现了
pass
lambda表达式
python里的匿名函数,格式:
lambda 参数:操作(参数)
a = [(1, 2), (4, 1), (9, 10), (13, -3)]
a.sort(key=lambda x: x[1])
print(a)
#Output: [(13, -3), (4, 1), (1, 2), (9, 10)]
enumerate
带索引的枚举,默认下标从0开始,但可以传参调整之。如
enumerate(my_list, 1)
下标就是从1开始
建立元组列表,list(enumerate(my_list, 1))
Map,Filter 和 Reduce
三个函数为函数式编程而生
Map
格式map(function_to_apply, list_of_inputs)
#配合匿名函数
map(lambda x:2*x, list)
Reduce
归约
#求乘积
reduce( (lambda x, y: x * y), [1, 2, 3, 4] )
Filter
filter(lambda x: x < 0, list1)