十. 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.


在计算机科学中,语法糖(syntactic sugar)是指编程语言中
可以更容易地
表达
一个操作
的语法


装饰器扩展至类:

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)--装饰器的更多相关文章

  1. python基础—函数装饰器

    python基础-函数装饰器 1.什么是装饰器 装饰器本质上是一个python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能. 装饰器的返回值是也是一个函数对象. 装饰器经常用于有切 ...

  2. Day11 Python基础之装饰器(高级函数)(九)

    在python中,装饰器.生成器和迭代器是特别重要的高级函数   https://www.cnblogs.com/yuanchenqi/articles/5830025.html 装饰器 1.如果说装 ...

  3. [python基础]关于装饰器

    在面试的时候,被问到装饰器,在用的最多的时候就@classmethod ,@staticmethod,开口胡乱回答想这和C#的static public 关键字是不是一样的,等面试回来一看,哇,原来是 ...

  4. 1.16 Python基础知识 - 装饰器初识

    Python中的装饰器就是函数,作用就是包装其他函数,为他们起到修饰作用.在不修改源代码的情况下,为这些函数额外添加一些功能,像日志记录,性能测试等.一个函数可以使用多个装饰器,产生的结果与装饰器的位 ...

  5. 学习PYTHON之路, DAY 5 - PYTHON 基础 5 (装饰器,字符格式化,递归,迭代器,生成器)

    ---恢复内容开始--- 一 装饰器 1 单层装饰器 def outer(func): def inner(): print('long') func() print('after') return ...

  6. 【Python基础】装饰器的解释和用法

    装饰器的用法比较简单,但是理解装饰器的原理还是比较复杂的,考虑到接下来的爬虫框架中很多用到装饰器的地方,我们先来讲解一下. 函数 我们定义了一个函数,没有什么具体操作,只是返回一个固定值 请注意一下缩 ...

  7. python基础之 装饰器,内置函数

    1.闭包回顾 在学习装饰器之前,可以先复习一下什么是闭包? 在嵌套函数内部的函数可以使用外部变量(非全局变量)叫做闭包! def wrapper(): money =10 def inner(num) ...

  8. python基础-----函数/装饰器

    函数 在Python中,定义一个函数要使用def语句,依次写出函数名.括号.括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用return语句返回. 函数的优点之一是,可以将代码块与主程 ...

  9. python基础之装饰器(实例)

    1.必备 #### 第一波 #### def foo(): print 'foo' foo #表示是函数 foo() #表示执行foo函数 #### 第二波 #### def foo(): print ...

随机推荐

  1. fiddler学习笔记&&基本使用

    周末在网上找了些fiddler相关的资料来看,学习下如何使用这个工具(平时接口测试用得比较多,在没有接口文档的情况下,可以通过抓包工具来提取需要测试的接口,ps.好久没写博客了,争取5月结束前再写2篇 ...

  2. centos 7 安装TensorFlow

    查看linux版本 uname -a 查看磁盘大小 准备好python 2.7 查看python版本  import sysprint sys.version print sys.version_in ...

  3. 高并发之限流RateLimiter(二)

    Guava RateLimiter提供了令牌桶算法实现:平滑突发限流(SmoothBursty)和平滑预热限流(SmoothWarmingUp)实现. SmoothBursty:令牌生成速度恒定 @T ...

  4. Python自学:第二章 数字 整数

    >>>2 + 3 5 >>>3 - 2 1 >>>3 * 2 6 >>>3 / 2 1.5

  5. LeetCode--012--整数转罗马数字(java)

    罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并 ...

  6. 倒排索引(Inverted Index)

    倒排索引(Inverted Index) 倒排索引是一种索引结构,它存储了单词与单词自身在一个或多个文档中所在位置之间的映射.倒排索引通常利用关联数组实现.它拥有两种表现形式: inverted fi ...

  7. IntelliJ IDEA 第一个 Scala 程序

    IntelliJ 安装完成 Scala 插件后,你需要尝试使用 IntelliJ 来创建并且运行第一个程序. 通常这个程序只是简单的输出 Hello World. 创建一个新工程 在文件下面选择新建, ...

  8. day11-15,装饰器

    day11 1.装饰器 import time # print(time.time()) # 点数前边是从1970年到现在过了多少秒 # time.sleep(10) # 让程序执行到这里停一会儿 # ...

  9. wordpress +window 走起~

    一.安装XAMPP   1 百度搜索xampp后下载安装到D盘(安装时按默认勾选安装即可) 2 安装完后,点击Start来启动Apache和MySQL这两个服务 3 如果Apache服务不能启动,多数 ...

  10. [LeetCode] 108. Convert Sorted Array to Binary Search Tree ☆(升序数组转换成一个平衡二叉树)

    108. Convert Sorted Array to Binary Search Tree 描述 Given an array where elements are sorted in ascen ...