十. 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 ...
随机推荐
- jmap -histo pid 输出的[C [B [I [S 的含义
JMAP 输出 其中: [C is a char[][S is a short[][I is a int[][B is a byte[][[I is a int[][]
- Superclass和Constructor Chaining
A subclass inherits accessible date fields and methods from its superclass. Does it inherit construc ...
- LeetCode--401--二进制手表
问题描述: 二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右侧. 例如,上面的二进制手表读取 “3 ...
- (Gorails) activeStore模块,把一堆属性放在一个hash对象内。gem 'activerecord-typedstore'增强了store模块,更好用了
https://api.rubyonrails.org/classes/ActiveRecord/Store.html https://gorails.com/episodes/preferences ...
- tomcat8 tomcat-users相关配置
第一步:修改账号密码 vim conf/tomcat-users.xml <role rolename="manager-gui"/> <role rolenam ...
- js 异步加载
document 加载 document.write("<scr" + "ipt src=\"js/jquery.js\"></sc ...
- PyCharm + PyQt4 环境搭建
一.准备工作 下载pycharm 和 pyqt4 (百度下就有) pyqt4安装好后,在C:\Python27\Lib\site-packages\PyQt4 路径下会有designer.exe ,这 ...
- 2018"百度之星"程序设计大赛 - 资格赛 A/B/E/F
调查问卷 Accepts: 505 Submissions: 2436 Time Limit: 6500/6000 MS (Java/Others) Memory Limit: 262144/ ...
- ArrayList详细
(IList值的集合 索引访问 ArrayList类IDictionary:键/值对 HashTable类 可变的集合 长度自动增长) ICollection-----IEnumerable--- I ...
- Git Diff 格式分析
参考: http://stackoverflow.com/questions/2529441/how-to-read-the-output-from-git-diff https://www.git- ...