十. Python基础(10)--装饰器
十. Python基础(10)--装饰器
1 ● 装饰器
|
A decorator is a function that take a function as an argument and return a function. We can use a decorator to extend the functionality of an existing function without changing its source. Decorators are syntactic sugar. 装饰器是一个函数, 它接受一个函数作为实参并且返回一个函数. 我们可以用装饰器来扩充一个已经存在的函数的功能, 但又不修改其源码. 装饰器是一种语法糖(syntactic sugar). ※ The "@" symbol indicates to the parser that we're using a decorator while the decorator function name references a function by that name. ※ |
|
※ A decorator in Python is any callable Python object that is used to modify a function or a class. A reference to a function "func" or a class "C" is passed to a decorator and the decorator returns a modified function or class. The modified functions or classes usually contain calls to the original function "func" or class "C". |
2 ● 装饰器的模板
|
def wrapper(func): # wrapper是装饰器的名字 def inner(*args, **kwargs): print("被装饰的函数执行之前你要做的事.") ret = func(*args, **kwargs) # 被装饰的函数执行, 返回值为None也写出来 print("被装饰的函数执行之后你要做的事.") return ret # ret有可能是None, 并且没有变量接收 return inner
@wrapper # 相当于welcome = wrapper(welcome) def welcome(name): # welcome是被装饰的函数 print('Welcome:%s!'%name)
@wrapper def home(): # home是被装饰的函数 print('欢迎来到home页!')
welcome("Arroz") print("===============================") home() |
|
参见: http://blog.csdn.net/qq_34409701/article/details/51589736 |
|
def wrapper(func): def inner(*args, **kwargs): print("AAAAAAA") print(func(*args, **kwargs)) return inner
@wrapper def f1(): return "f1"
f1()
''' AAAAAAA f1 ''' |
知识补充:
1 ● eval()函数
|
if name in locals(): return eval(name)(*args) |
|
※ name是函数名, 后面的形参需要和一般函数名一样带括号(*args) |
2 ● 斐波那契数列停止的情况:
|
# ① 长度小于多少 ''' l=[1,1] while len(l) < 20: l.append(l[-1]+l[-2]) print(l) ''' # ② 最后一个数小于多少 ''' l = [1,1] while l[-1] < 40000: l.append(l[-1] + l[-2]) print(l) ''' |
十. Python基础(10)--装饰器的更多相关文章
- python基础—函数装饰器
python基础-函数装饰器 1.什么是装饰器 装饰器本质上是一个python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能. 装饰器的返回值是也是一个函数对象. 装饰器经常用于有切 ...
- Day11 Python基础之装饰器(高级函数)(九)
在python中,装饰器.生成器和迭代器是特别重要的高级函数 https://www.cnblogs.com/yuanchenqi/articles/5830025.html 装饰器 1.如果说装 ...
- [python基础]关于装饰器
在面试的时候,被问到装饰器,在用的最多的时候就@classmethod ,@staticmethod,开口胡乱回答想这和C#的static public 关键字是不是一样的,等面试回来一看,哇,原来是 ...
- 1.16 Python基础知识 - 装饰器初识
Python中的装饰器就是函数,作用就是包装其他函数,为他们起到修饰作用.在不修改源代码的情况下,为这些函数额外添加一些功能,像日志记录,性能测试等.一个函数可以使用多个装饰器,产生的结果与装饰器的位 ...
- 学习PYTHON之路, DAY 5 - PYTHON 基础 5 (装饰器,字符格式化,递归,迭代器,生成器)
---恢复内容开始--- 一 装饰器 1 单层装饰器 def outer(func): def inner(): print('long') func() print('after') return ...
- 【Python基础】装饰器的解释和用法
装饰器的用法比较简单,但是理解装饰器的原理还是比较复杂的,考虑到接下来的爬虫框架中很多用到装饰器的地方,我们先来讲解一下. 函数 我们定义了一个函数,没有什么具体操作,只是返回一个固定值 请注意一下缩 ...
- python基础之 装饰器,内置函数
1.闭包回顾 在学习装饰器之前,可以先复习一下什么是闭包? 在嵌套函数内部的函数可以使用外部变量(非全局变量)叫做闭包! def wrapper(): money =10 def inner(num) ...
- python基础-----函数/装饰器
函数 在Python中,定义一个函数要使用def语句,依次写出函数名.括号.括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用return语句返回. 函数的优点之一是,可以将代码块与主程 ...
- python基础之装饰器(实例)
1.必备 #### 第一波 #### def foo(): print 'foo' foo #表示是函数 foo() #表示执行foo函数 #### 第二波 #### def foo(): print ...
随机推荐
- python中文件的读和写操作
一.打开文件 data = open("yesterday",encoding="utf-8").read() # python默认的打字符编码是unicode ...
- maven 把本地jar包打进本地仓库
maven 把本地jar包打进本地仓库 1.本地有自己写的项目jar包,但是需要用maven依赖对其进行引用: 2.某个jar包在远程仓库没有,导致pom.xml报错,此时可以从网上单独下载此jar包 ...
- Channel L 自然拼读法 Teacher:Lamb
# 今日学习的 bl fl pl cl gl br cr gr pr fr tr dr # 上课内容 was he <--- h在中间出现的情况连读 was he 这里的 ...
- Winform开发框架之框架演化
Winform开发框架方面的文章我介绍很多了,有宏观介绍,也有部分技术细节的交流,每次我希望能从不同角度,不同方面来介绍我的WInform开发框架,这些其实都是来源于客户的需求,真实的项目场景.本文主 ...
- 20170928xlVBA自定义分类汇总
SubtotalByCQL Range("A1:E100").Value, "Select 1,2,Sum(4),Count(4) GroupBy 1,2", ...
- You Don't Know JS: Scope & Closures (第一章:什么是Scope)
Content What is Scope? Lexical Scope Function Vs. Block Scope Hoisting Scope Closures Appendix: Dyna ...
- android AIDL 语言用法
跨进程通信可以用AIDL语言 这里讲述下如何使用AIDL语言进行跨进程通信 文章参考 <设计模式>一书 demo结构参考 主要的文件类有:IBankAidl.aidl java文件:Aid ...
- Postman发包form-data、x-www-form-urlencoded、raw、binary的区别
首先普及下http的Post四种Content-Type Postman中post编码方式form-data.x-www-form-urlencoded.raw.binary的区别 x-www-for ...
- vue基础 (三) 自动化工具(Vue CIL)
一.自动化工具(Vue CIL) 安装过程 1. 先安装nvm 参考:https://www.jianshu.com/p/d0e0935b150a https://www.cnblogs.com/tj ...
- CentOS7.3环境下源码安装httpd
CentOS7.3环境下源码安装httpd 本文在CentOS7.3下,源码安装apache服务httpd2.4. 1.下载好源码安装包 [root@localhost ~]#ll total 625 ...