1.装饰器

本质是函数,(装饰其他函数)就是为其他函数添加附加功能

原则:

  1)不能修改被装饰的函数的源代码
  2)不能修改被装饰的函数的调用方式

2.实现装饰器知识储备

  1)函数即“变量”

  2)高阶函数

    a.把一个函数当作实参传给另一个函数(可以做到不修改被装饰函数的源代码的情况下为其添加功能)

    b.返回值中包含函数名)(不修改函数的调用方式)

  3)嵌套函数

3.高阶函数+嵌套函数=》装饰器

4.初步装饰器

import time
def timer(func): #timer(test1) func = test1
def deco():
start_time = time.time()
func() #run test1
stop_time = time.time()
print('the func run time is %s'%(stop_time-start_time))
return deco @timer #test1 = timer(test1)
def test1():
time.sleep()
print('in the test1')
test1()

5.功能比较完善的装饰器

user,passwd = 'hsj',''

def auth(auth_type):
print('auth func:',auth_type)
def outer_wrapper(func):
def wrapper(*args, **kwargs):
print('wrapper:',*args, **kwargs)
if auth_type == 'local':
username = input('Username:').strip()
password = input('Password:').strip() if username == user and password == passwd:
print('\033[32;1mUser has pass authentication\033[0m')
return func(*args, **kwargs) # from home #函数wrapper的返回值
else:
exit('\033[31;1mInvalid username or password\033[0m')
elif auth_type =='ldap':
print('ldappppppppp')
return wrapper
return outer_wrapper def index():
print('welcome to index psge')
@auth(auth_type='local') # 加了括号相当于运行了outer_wraper所以会执行里面内容 # home = auth(home)
def home():
print('welcome to home page')
return 'from home' #这里有返回值在装饰器里也应该有个返回值不然是print()不出来的
@auth(auth_type='ldap')
def bbs():
print('welcome to bbs page') index()
print(home())
bbs()

python 3.x 学习笔记5 (装饰器)的更多相关文章

  1. Python学习笔记:装饰器

    Python 装饰器的基本概念和应用 代码编写要遵循开放封闭原则,虽然在这个原则是用的面向对象开发,但是也适用于函数式编程,简单来说,它规定已经实现的功能代码不允许被修改,但可以被扩展,即: 封闭:已 ...

  2. Python学习笔记012——装饰器

    1 装饰器 1.1装饰器定义 在代码运行期间动态增加功能的方式,称之为“装饰器”(Decorator). 1.2 装饰器分类 装饰器:函数装饰器,类装饰器,函数的装饰器,类的装饰器 装饰器:函数装饰函 ...

  3. python学习笔记:装饰器2

    python的装饰器本质是函数,为了不改变装饰目标函数内部代码而增加额外功能而存在 一.一般装饰函数实例: import datetime def func_name(func):#定义一个装饰函数, ...

  4. python学习笔记(五):装饰器、生成器、内置函数、json

    一.装饰器 装饰器,这个器就是函数的意思,连起来,就是装饰函数,装饰器本身也是一个函数,它的作用是用来给其他函数添加新功能,比如说,我以前写了很多代码,系统已经上线了,但是性能比较不好,现在想把程序里 ...

  5. python学习笔记之装饰器、生成器、内置函数、json(五)

    一.装饰器 装饰器,这个器就是函数的意思,连起来,就是装饰函数,装饰器本身也是一个函数,它的作用是用来给其他函数添加新功能比如说,我以前写了很多代码,系统已经上线了,但是性能比较不好,现在想把程序里面 ...

  6. python学习笔记之装饰器、递归、算法(第四天)

    参考老师的博客: 金角:http://www.cnblogs.com/alex3714/articles/5161349.html 银角:http://www.cnblogs.com/wupeiqi/ ...

  7. Python学习笔记之装饰器原理

    def decorator(fn): def wrapper(): print("询价") fn() print("购买成功!") return wrapper ...

  8. python笔记 - day4-之装饰器

                 python笔记 - day4-之装饰器 需求: 给f1~f100增加个log: def outer(): #定义增加的log print("log") ...

  9. python网络爬虫学习笔记

    python网络爬虫学习笔记 By 钟桓 9月 4 2014 更新日期:9月 4 2014 文章文件夹 1. 介绍: 2. 从简单语句中開始: 3. 传送数据给server 4. HTTP头-描写叙述 ...

  10. Python基础(七) python自带的三个装饰器

    说到装饰器,就不得不说python自带的三个装饰器: 1.@property   将某函数,做为属性使用 @property 修饰,就是将方法,变成一个属性来使用. class A(): @prope ...

随机推荐

  1. 【BZOJ3270】博物馆 概率DP 高斯消元

    链接: #include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网 ...

  2. Process Explorer

    https://docs.microsoft.com/en-us/sysinternals/downloads/process-explorer Ever wondered which program ...

  3. iOS (封装)一句话调用系统的alertView和alertController

    前言: 本文仅作参考存留,请用新版封装:iOS 更加优雅便捷的UIAlertView/UIAlertController封装使用 UIAlertController是iOS8.0之后出来的新方法,其将 ...

  4. 计算sigma

    1.计算平均值Avg Avg = (a0 + a1 + ......+ an-1) / n 2.计算sigma sigma = sqrt( ( (a0-avg) ^2   + (a1-avg) ^2 ...

  5. spring boot integrated mybatis three ways!--转

    https://github.com/spring-projects/spring-boot/issues/5400 一.使用mybatis-spring-boot-starter1.添加依赖 org ...

  6. Android 那些年,处理getActivity()为null的日子

    在日常开发中的时候,我们经常会使用ViewPager+Fragment进行视图滑动,在某些部分逻辑也许我们需要利用上下文Context(例如基本的Toast),但是由于Fragment只是衣服在Act ...

  7. Sed Awk 日常使用总结

    Sed命令语法sed [option] {sed-commands}{input-file}sed首先从input-file中读取第一行,然后执行所有的sed-commands:再读取第二行,执行所有 ...

  8. nginx 子进程 woker process 启动失败的问题

    问题: 重启nginx服务,worker process 子进程启动失败,启动的都是master进程: 负载急速升高(平常都是4-5),占用CPU资源多的前十进程都是nginx : nginx 错误日 ...

  9. java 通过cookie判断是否登陆

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOEx ...

  10. rescan-scsi-bus.sh linux扫盘 脚本

    [root@ftp:/home/tools/shell] > yum install sg3_utils* Loaded plugins: fastestmirror Repository ba ...