python装饰器复习
Flask – 简单易扩展
flask不包含数据库抽象层,表单验证,或者任何其他现有库可以胜任的东西,
众多的扩展提供了数据库集成,表单验证,上传处理,多种开放认证技术的功能。
不同于django,flask需要自己动手配置的东西很多,但同于游戏,自由度更高!
----取自《Flask–中文文档》
本文为flask初学做准备 链接
学习之前—复习python装饰器
装饰器 – Decorators
python的一个重要部分,作用:修改函数的功能函数
可使代码简洁明了,不脏不乱!
1.将函数用赋值的方式调用,del 函数名,删除后,其变量名可以继续调用;
def one(name="Tom"):
return "hellow!"+name
two = one
print(two)
# outputs: 'hellow! Tom'
del one #执行删除
print(one)
#outputs: NameError
print(two)
#outputs:'hellow! Tom'
- 在函数中定义函数;函数内调用分全局和局部哦!
def one(name="Tom"):
print(one)
def one_one():
return "one_one"
def one_two():
reutrn "one_two"
one_one()
one_two()
one()
#output:one,one_one,one_two
one_one()
#output:NameError:name'one_one' is not defind
- 注意调用函数时,函数名();
def one(name="one"):
def one_one():
return name
def one_two():
return "two"+name
if name == "one":
return one_one
else:
return one_two
a = one()
print(a)
outputs:<function one at 0x7f2143c01500>
print(a())
#outputs: one
b= one(name="two")
print(b())
#outputs:two
print(one()())
#outputs: one
- 函数之间的传值;
def one(name="Tom"):
return "Tom"
def two(name):
return "two"+name
two(one)
#outputs: twoTom
- 现在创建第一个装饰器:用两个函数实现输出print(“jdjango介绍”),print(“python爬虫介绍”),print(“python数据分析介绍”),通过python数据分析介绍,引出其他两个 ;
def one(text):
#one_one函数是介绍有关python的全部知识
def one_one():
print("django介绍")
text()
print("python爬虫介绍")
return one_one
#two函数是介绍有关python的全部知识
def two():
print("python数据分析介绍")
#包装one函数 :
a = one(two)
#执行:
a()
#output: hellow! python
-----装饰器简单的理解; 通过颜色可以区别苹果的种类,颜色便是装饰器,通过了解python数据分析介绍,便可以发现更多有关python的知识,这里的two函数便是装饰器
6.上面便是简单的装饰器介绍,接下来为真正的装饰器;
def one(text):
#one_one函数是介绍有关python的全部知识
def one_one():
print("django介绍")
text()
print("python爬虫介绍")
return one_one
@one
def two():
print("这是two函数的内容")
#output: django介绍 这里是two函数的介绍 python爬虫介绍
#运行以下代码
print(two.__name__)
#output: one_one
#问题是我们需要得出的是其本函数two,官方给出了wraps方法
from functools import wraps
def one(text):
#one_one函数是介绍有关python的全部知识
@wraps(text)
def one_one():
print("django介绍")
text()
print("python爬虫介绍")
return one_one
@one
def two():
print("two")
print(two.__name__)
#output: two
- 一个完整的装饰器;
from functools import wraps
def one(name):
@wraps(name)
def one_one(*args,**kwargs):
if not a:
return "a不存在"
return one_one
@one
def two():
return "a是存在的"
a = True
print(two())
#output: a是存在的
a = False
print(two())
#output: a不存在
注意: @wraps 接收一个函数来进行修饰,并加入了复制函数名称,注释文档,参数列表,等等功能,这就可以让我们装饰器上访问之前函数的属性了
- 具体应用 – 日志,等等。
#以下为在日志上的应用
from functools import wraps
def logit(func):
@wraps(func)
def with_logging(*args, **kwargs):
a = 4
print(func.__name__ + " 函数输入%i得出打的结果为:" %a+ ""+str(func(a)))
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 函数输入4得出打的结果为:8
注意: 把logit()函数放在一个函数中,将addition_func()函数上的@logint 改为这个函数名,运行起来跟以上代码一样哦
- 类装饰器用法 ,这里给大家写一个 发邮件示例
from functools import wraps
class log(object):
def __init__(self,logd):
self.log=log
def __call__(self,func):
@wraps(func)
def wraps_function(*args,**kwargs):
print(func.__name__+"函数启用")
with open(self.log)
self.notify()
return wraps_function
def notify(self):
#log只打日志
pass
@log()
def myone(text):
return text
10.如果忘了 怎么调用父类函数,下面就是哦!!!
#基本示例
class one(object):
name = ""
age = 0
def __init__(self,text,times):
self.text = text
self.times = times
def __call__(self):
print(self.text,self.times)
class two(one):
no = ""
def __init__(self,text,times,email):
super().__init__(text,times)
self.email = email
def show(self):
print(self.text,self.times,self.email)
# def email_write(text,email,times):
# return text,email,times
two("嘿,今天星期几!",time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),"1351316701@qq.com").show()
相信到这里您就可以轻松使用了,快去练习把(本文为作者理解复习所编,仅供参考,QQ:1351316701 ,第一次写,大家多提提建议哦)