python 装饰器和软件目录规范一
1、装饰器和迭代器的概念。
装饰器本质是一个函数,是为其他函数添加附加功能。
原则:不修改原函数源代码
不修改原函数的调用方式
2、装饰器的简单应用
# Author : xiajinqi
import time
def timmer(func):
def wrapper(*args,**kwargs):
start_time = time.time()
func()
stop_time =time.time()
print("执行时间为 %s" %(stop_time - start_time))
return wrapper @timmer
def loggin():
time.sleep(1)
print("hello world") loggin()
3、理解装饰器的知识需要储备 :
函数即为变量、高阶函数 、嵌套函数
4、 函数在内存中定义。深入理解函数即为变量的概念.

需要区分定义和调用是两步进行的。必须要先定义再调用
def foo():
print("foo")
bar()
def bar():
print("bar") foo() E:\Users\xiajinqi\PycharmProjects\Atm\venv\Scripts\python.exe E:/Users/xiajinqi/PycharmProjects/Atm/bin/test2.py
foo
bar Process finished with exit code 0 # Author : xiajinqi def foo():
print("foo")
bar() #foo 执行时候bar还没有定义所以执行会报错
foo()
def bar():
print("bar")
foo()
bar() E:\Users\xiajinqi\PycharmProjects\Atm\venv\Scripts\python.exe E:/Users/xiajinqi/PycharmProjects/Atm/bin/test2.py
foo
Traceback (most recent call last):
File "E:/Users/xiajinqi/PycharmProjects/Atm/bin/test2.py", line 6, in <module>
foo()
File "E:/Users/xiajinqi/PycharmProjects/Atm/bin/test2.py", line 5, in foo
bar()
NameError: name 'bar' is not defined Process finished with exit code 1
5、函数即变量和高阶函数,高阶函数,将一个函数作为变量参数传递给另外一函数,返回值中包含函数。(可以实现装饰器的第一步,把改变代码添加功能,但是会改变调用方式还不是一个真正的装饰器)
#高阶函数
# Author : xiajinqi
import time
def foo():
time.sleep(1)
print("foo")
return 0 def test(func):
start_time = time.time()
func()
end_time = time.time()
print("tims is %s" %(end_time -start_time )) test(foo) #相当于给foo加了一个附加功能 但是还是不符合装饰器规则。改变了调用规则 #高阶函数 传递 test(foo)
# Author : xiajinqi
import time
def foo():
time.sleep(1)
print("foo")
return 0 def test(func):
print(func)
func()
return func foo=test(foo) # 在赋值时候,foo 作为地址传递给test函数,此处相当于同时调用了两个函数test和foo(test内部调用foo),所以会比foo更多的功能
foo() # 这时候的foo已经是新的foo了。
6、嵌套函数,在一个函数体内调用另外一个函数
#高阶函数 传递 test(foo)
# Author : xiajinqi
def test():
print('test')
def bar() :
print('bar')
return bar #bar()内部调用 # 外部调用
bar=test()
bar()
7、函数的作用域
# 全局变量和局部变量
def test1():
print("test1")
def test2():
print("test2")
def test3():
print("test3")
test3() test1() #test2由于没有调用,所以test3也不会调用
8、装饰器的实现
# 装饰器,三个函数,要求实现统计三个函数的运行时间,在函数运行的时候,会自动统计函数的运行时间,要求不修源代码
import time #装饰器
def deco(func):
start_time = time.time()
return func
end_time = time.time()
print("运行时间 %s" %(end_time-start_time)) def timeer(func):
def deco():
start_time = time.time()
func()
end_time = time.time()
print("运行时间 %s" % (end_time - start_time))
return deco @timeer
def test1():
time.sleep(1)
print("test1") @timeer
def test2():
time.sleep(1)
print("test2")
@timeer
def test3():
time.sleep(1)
print("test3") #test1=timeer(test1) #思考timemer存在的含义 timmer调用什么也没有做,传递func给deco
#test1() #相当于执行deco,但是如果直接调用deco 就会改变调用方式。因此需用timmer可以达到 test2()
9 、高级装饰器:
# 装饰器,三个函数,要求实现统计三个函数的运行时间,在函数运行的时候,会自动统计函数的运行时间,要求不修源代码
import time #装饰器 def timeer(func):
def deco(*args,**kwargs):
start_time = time.time()
func(*args,**kwargs) # 不定参数传递
end_time = time.time()
print("运行时间 %s" % (end_time - start_time))
return deco @timeer
def test1():
time.sleep(1)
print("test1") @timeer
def test2(name):
time.sleep(1)
print("test2 %s" %(name))
@timeer
def test3():
time.sleep(1)
print("test3") #test1=timeer(test1) #思考timemer存在的含义 timmer调用什么也没有做,传递func给deco
#test1() #相当于执行deco,但是如果直接调用deco 就会改变调用方式。因此需用timmer可以达到 test1()
test2("xiajinqi")
10、装饰器高潮,模拟网站登录,三个页面,其中两个需要加上验证功能
# Author : xiajinqi # 装饰器,三个函数,要求实现统计三个函数的运行时间,在函数运行的时候,会自动统计函数的运行时间,要求不修源代码
import time #装饰器 def auth(func):
def deco(*args,**kwargs):
print("func")
username = input("name:")
passwd = input("passwd:")
if username == 'xiajinqi' and passwd == '':
func(*args,**kwargs)
else :
print("认识失败")
return deco def index():
print("index of xiajinqi") @auth
def home(name):
print("index of home %s" %(name)) @auth
def bbs():
print("index of xiajinqi") index()
home("xiajinqi")
bbs()
python 装饰器和软件目录规范一的更多相关文章
- Day04 - Python 迭代器、装饰器、软件开发规范
1. 列表生成式 实现对列表中每个数值都加一 第一种,使用for循环,取列表中的值,值加一后,添加到一空列表中,并将新列表赋值给原列表 >>> a = [0, 1, 2, 3, 4, ...
- Day4 - Python基础4 迭代器、装饰器、软件开发规范
Python之路,Day4 - Python基础4 (new版) 本节内容 迭代器&生成器 装饰器 Json & pickle 数据序列化 软件目录结构规范 作业:ATM项目开发 ...
- Python基础4 迭代器、装饰器、软件开发规范
本节内容 迭代器&生成器 装饰器 Json & pickle 数据序列化 软件目录结构规范 作业:ATM项目开发 1.列表生成式,迭代器&生成器 列表生成式 孩子,我现在有个需 ...
- Python之迭代器、装饰器、软件开发规范
本节内容 迭代器&生成器 装饰器 Json & pickle 数据序列化 软件目录结构规范 作业:ATM项目开发 1.列表生成式,迭代器&生成器 列表生成式 孩子,我现在有个需 ...
- Python_Day5_迭代器、装饰器、软件开发规范
本节内容 迭代器&生成器 装饰器 Json & pickle 数据序列化 软件目录结构规范 1.列表生成式,迭代器&生成器 列表生成 >>> a = [i+1 ...
- py基础4--迭代器、装饰器、软件开发规范
本节内容 迭代器&生成器 装饰器 Json & pickle 数据序列化 软件目录结构规范 作业:ATM项目开发 1. 列表生成式,迭代器&生成器 列表生成式 我现在有个需求, ...
- python 装饰器、递归原理、模块导入方式
1.装饰器原理 def f1(arg): print '验证' arg() def func(): print ' #.将被调用函数封装到另外一个函数 func = f1(func) #.对原函数重新 ...
- Python 装饰器入门(上)
翻译前想说的话: 这是一篇介绍python装饰器的文章,对比之前看到的类似介绍装饰器的文章,个人认为无人可出其右,文章由浅到深,由函数介绍到装饰器的高级应用,每个介绍必有例子说明.文章太长,看完原文后 ...
- 你必须学写 Python 装饰器的五个理由
你必须学写Python装饰器的五个理由 ----装饰器能对你所写的代码产生极大的正面作用 作者:Aaron Maxwell,2016年5月5日 Python装饰器是很容易使用的.任何一个会写Pytho ...
随机推荐
- C#调用C++函数
一.新建C++项目 1.在VS2012中新建->项目->模版->其他语言->Win32->Win32项目->下一步->选DLL,导出符号. 2.在XX.h项目 ...
- [翻译] SvpplyTable
SvpplyTable https://github.com/liuminqian/SvpplyTable SvpplyTable is a demo to realize expandable an ...
- 进程&多道技术
进程 顾名思义,进程即正在执行的一个过程.进程是对正在运行程序的一个抽象. 进程的概念起源于操作系统,是操作系统最核心的概念,也是操作系统提供的最古老也是最重要的抽象概念之一.操作系统的其他所有内容都 ...
- Python入门学习网址
Python入门学习网址:http://www.runoob.com/python/python-install.html
- 乘风破浪:LeetCode真题_017_Letter Combinations of a Phone Number
乘风破浪:LeetCode真题_017_Letter Combinations of a Phone Number 一.前言 如何让两个或者多个集合中的随机挑选的元素结合到一起,并且得到所有的可能呢? ...
- 沉淀再出发:在python3中导入自定义的包
沉淀再出发:在python3中导入自定义的包 一.前言 在python中如果要使用自己的定义的包,还是有一些需要注意的事项的,这里简单记录一下. 二.在python3中导入自定义的包 2.1.什么是模 ...
- Asp.Net MVC Identity 2.2.1 使用技巧(六)
使用用户管理器之角色管理 一.建立模型,这里我们其实在之前的技巧(五)已经建好了. 二.建立控制器RolesAdminController 1.在controllers文件夹上点右键>添加> ...
- iOS自动化-- 常用iOS命令
iOS命令: 获取设备的的UDID idevice_id --list # 显示当前所连接设备的 udid instruments -s devices # 列出所有设备,包括真机.模拟器.mac i ...
- 如何将本地项目上传至GitHub
首先你需要一个github账号,所有还没有的话先去注册吧! https://github.com/ 我们使用git需要先安装git工具,这里给出下载地址,下载后一路直接安装即可: https://gi ...
- Alpha Scrum3
Alpha Scrum3 牛肉面不要牛肉不要面 Alpha项目冲刺(团队作业5) 各个成员在 Alpha 阶段认领的任务 林志松:音乐网页前端页面编写,博客发布 林书浩.陈远军:界面设计.美化 吴沂章 ...