定义:本质上就是个函数,(装饰器其他函数)就是为了给其他函数添加附加功能

原则:1.不能修改被装饰的函数的源代码

2.不能修改被装饰的函数的调用方式

 #-*-coding:utf-8-*-
1 import time
def timer(hello):
def func(*args,**kwargs): #函数传参,不限个数。
start = time.time()
hello(*args,**kwargs) #函数传参,不限个数。
end = time.time()
print("运行时间:%s"%(end - start))
return func
@timer
def hello():
time.sleep(2)
print("nihao")
hello()

注:装饰器得写在被装饰函数的上面。

小实验:密码验证

#-*-coding:utf-8-*-
import time
user = {                           #存储用户名和密码
"luozeng":'',
"xuemanfei":'',
"xutian":''
} def yanzheng(hello):
def func(*args,**kwargs):
start = time.time()
username = input("请输入用户:").strip() #用户输入
password = input("请输入密码:").strip()
if username in user and password == user[username]: #用户名和密码验证
print("登陆成功")
hello(*args,**kwargs)
else:
exit("用户名或密码错误!")
end = time.time()
print("运行时间:%s"%(end - start))
return func
@yanzheng
def hello():
print("你好!")
hello()

python装饰器学习笔记的更多相关文章

  1. Python 装饰器学习

    Python装饰器学习(九步入门)   这是在Python学习小组上介绍的内容,现学现卖.多练习是好的学习方式. 第一步:最简单的函数,准备附加额外功能 1 2 3 4 5 6 7 8 # -*- c ...

  2. (转载)Python装饰器学习

    转载出处:http://www.cnblogs.com/rhcad/archive/2011/12/21/2295507.html 这是在Python学习小组上介绍的内容,现学现卖.多练习是好的学习方 ...

  3. Python装饰器学习

    Python装饰器学习(九步入门)   这是在Python学习小组上介绍的内容,现学现卖.多练习是好的学习方式. 第一步:最简单的函数,准备附加额外功能 ? 1 2 3 4 5 6 7 8 # -*- ...

  4. Python 装饰器学习心得

    最近打算重新开始记录自己的学习过程,于是就捡起被自己废弃了一年多的博客.这篇学习笔记主要是记录近来看的有关Python装饰器的东西. 0. 什么是装饰器? 本质上来说,装饰器其实就是一个特殊功能的函数 ...

  5. python 装饰器学习(decorator)

    最近看到有个装饰器的例子,没看懂, #!/usr/bin/python class decorator(object): def __init__(self,f): print "initi ...

  6. python装饰器学习详解-函数部分

    本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理 最近阅读<流畅的python>看见其用函数写装饰器部分写的很好,想写一些自己的读书笔记. ...

  7. Python装饰器学习(九步入门)

    这是在Python学习小组上介绍的内容,现学现卖.多练习是好的学习方式. 第一步:最简单的函数,准备附加额外功能 ? 1 2 3 4 5 6 7 8 # -*- coding:gbk -*- '''示 ...

  8. Python 装饰器学习以及实际使用场景实践

    前言 前几天在看Flask框架,对于非常神奇的@语法,不是非常的理解,回来补装饰器的功课.阅读很多的关于装饰器的文章,自己整理一下,适合自己的思路的方法和例子,与大家分享. app = Flask(_ ...

  9. python装饰器见解笔记

    def zsq(fun): def zsq_n(*args,**kwargs) print('这是装饰器需要运行内容') r = fun(*args,**kwargs) print('在被装饰函数执行 ...

随机推荐

  1. Python实现登陆的功能

    import datetimetoday=datetime.datetime.today()# 获取当前时间for i in range(3): username=input("请输入用户名 ...

  2. L260

    Innovative UK technology that can deliver drugs deep into the brain to treat neurological diseases, ...

  3. PMS5003ST+Arduino Nano OLED屏显示

    整合OLED显示和PMS5003报数 #include <Arduino.h> #include <pms.h> /////////////////////////////// ...

  4. js 自执行匿名函数(转载)

    自执行匿名函数: 常见格式:(function() { /* code */ })(); 解释:包围函数(function(){})的第一对括号向脚本返回未命名的函数,随后一对空括号立即执行返回的未命 ...

  5. REST easy with kbmMW #17 – Database 6 – Existing databases

    kbmMW已经包含了非常精细的功能来确定和解释数据库中表的元数据. 在下一版本中,这个功能将得到进一步加强,可以导入现有数据库中的表,自动创建与表相匹配的ORM实体类. 这意味着你能够使用kbmMW的 ...

  6. Python爬虫框架Scrapy实例(三)数据存储到MongoDB

    Python爬虫框架Scrapy实例(三)数据存储到MongoDB任务目标:爬取豆瓣电影top250,将数据存储到MongoDB中. items.py文件复制代码# -*- coding: utf-8 ...

  7. python模块大全

    python模块大全2018年01月25日 13:38:55 mcj1314bb 阅读数:3049 pymatgen multidict yarl regex gvar tifffile jupyte ...

  8. binarysearchtree

    public class binarytree<Value> { private Node root = null; private class Node{ private Value v ...

  9. 2016 多校联赛7 Joint Stacks (优先队列)

    A stack is a data structure in which all insertions and deletions of entries are made at one end, ca ...

  10. centos7 安装mysql--python模块出现EnvironmentError: mysql_config not found和error: command 'gcc' failed with exit status 1

    要想使python可以操作mysql 就需要MySQL-python驱动,它是python 操作mysql必不可少的模块. 下载地址:https://pypi.python.org/pypi/MySQ ...