十. 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 ...
随机推荐
- Win7无法保存共享帐户密码
每次机器重启完之后,网络共享的密码总是要重新输入. [记住我的凭据]选项不起作用. 查到了下面百度经验的文章,挺靠谱的. https://jingyan.baidu.com/article/59a01 ...
- 连接PL/SQL
1.登录PL/SQL Developer 这里省略Oracle数据库和PL/SQL Developer的安装步骤,注意在安装PL/SQL Developer软件时,不要安装在Program Files ...
- Life Winner Bo HDU - 5754
Bo is a "Life Winner".He likes playing chessboard games with his girlfriend G. The size of ...
- python基础之lambda,sort,filter,map,递归函数的运用
内容梗概:1. lamda 匿名函数2. sorted()3. filter()4. map()5. 递归函数 1.lambda 形式: lambda 参数:返回值 f = lambda x,y: x ...
- 2018 USP Try-outsF - Optimizing Transportation in Portugal
题意:给你一副无向图,求使s到t删掉一条的最短路最大的长度 题解:先预处理s,t到每个点的最短路,二分答案,对于一条边,如果选中这条边,那么对于s->u+u->v+v->t或者s-& ...
- 第二阶段——个人工作总结DAY04
1.昨天做了什么:实现所有需要跳转活动的点击事件. 2.今天打算做什么:打算把值能够传递过去. 3.遇到的困难:无
- http.cof
## This is the main Apache HTTP server configuration file. It contains the# configuration directives ...
- SQL - 常用的特殊查询
sql 查询某字段不重复的全部记录: select * from table where fid in(Select min(fid) FROM table group by name) :
- redis初认知(持续更新中)
一:redis是什么? 1).redis是一个基于内存的key-value数据库(存储系统). 2).Redis 是一个高性能的key-value数据库. 3).redis的存储分为内存存储.磁盘存储 ...
- 十八、Spring框架(AOP)
一.AOP(基于XML方式配置AOP) AOP(Aspect Oriented Program):面向切面编程.思想是:把功能分为核心业务功能和周边功能. 所谓核心业务功能:比如登录,增删改数据都叫做 ...