Python基础【装饰器】

装饰器:

装饰器:用来修改函数功能的函数

  • 可以在不改变原函数的基础上添加功能
    实现装饰器的方法:从函数中返回函数,将原函数作为一个参数传给另一个函数

    代码:装饰器pro_print在函数执行前输出提示"welcome to class"

    def pro_print(fun): # 装饰器函数
    def wrapper(*args,**kwargs): # 接收各种类型的不定长参数
    print("welcome to class")
    fun()
    return wrapper
    @pro_print # 语法糖,在函数前使用用于装饰函数
    def fun():
    print(‘hello,python‘)
    fun()

    但装饰器会覆盖原有函数的函数名,提示文档等信息

    def pro_print(fun): # 装饰器函数
    def wrapper(*args,**kwargs): # 接收各种类型的不定长参数
    ‘‘‘执行函数前提示welcom to class‘‘‘
    print("welcome to class")
    fun()
    return wrapper
    @pro_print # 语法糖,在函数前使用用于装饰函数
    def fun():
    ‘‘‘输出hello,python‘‘‘
    print(‘hello,python‘)
    fun()
    print(fun.__name__)
    print(fun.__doc__)

    测试结果:


导入functools中的wraps函数可以解决这个问题

from functools import wraps
def pro_print(fun): # 装饰器函数
@wraps(fun)
def wrapper(*args,**kwargs): # 接收各种类型的不定长参数
‘‘‘执行函数前提示welcom to class‘‘‘
print("welcome to class")
fun()
return wrapper
@pro_print # 语法糖,在函数前使用用于装饰函数
def fun():
‘‘‘输出hello,python‘‘‘
print(‘hello,python‘)
fun()
print(fun.__name__)

测试结果:

原文地址:http://blog.51cto.com/13992211/2330382

时间: 2024-07-30 07:55:28

Python基础【装饰器】的相关文章

python基础—装饰器

python基础-装饰器 定义:一个函数,可以接受一个函数作为参数,对该函数进行一些包装,不改变函数的本身. def foo(): return 123 a=foo(); b=foo; print(a) print(b) 123 <function foo at 0x000001608E2B97B8> 解释:把函数foo赋值给a和b,a 赋值的是调用后的函数,变量的值就是返回值.b 赋值的是调用前的函数,所以b 就是那个赋值的函数.函数本身+(),就是调用. callable(a) False

python基础-装饰器和偏函数

一.装饰器 1.概念:装饰器是一个闭包(内层函数引用外层函数的非全局变量,这个内层函数就可称之为闭包),把一个函数当做参数返回一个替代版的函数,本质上就是一个返回函数的函数. 2.作用:在不修改源代码的基础上,扩展代码的使用. 3.理解装饰器 #不带参数的源函数 def fun1(): print("I love python") fun1() #若我想在原来的基础上,先输出一行的*号,我们可以重新写一个函数 def fun2(): print("***********&qu

python基础===装饰器@property

以下来自Python 3.6.0 Document: class property(fget=None, fset=None, fdel=None, doc=None) Return a property attribute. fget is a function for getting an attribute value. fset is a function for setting an attribute value. fdel is a function for deleting an

python基础---装饰器

1,装饰器: 1)为什么要用装饰器:因为你开发的程序一旦上市,就要遵守源代码开放并且尽量不能修改源代码,函数的调用方式也尽量不要修改,新的需求来了,每一       款软件都是需要更新的,在不修改源代码,不修改函数调用方式,同时还要增加新的功能,怎么实现呢?所以有了装饰器,来满足我们的条件. 2)什么是装饰器:顾名思义,装饰就是修饰,器相当于函数 3)装饰器定义:其本质就是一个函数,功能是为其他函数添加新的一个功能. 2,举例: 1) 现在来建立一个最简单的函数,这组函数值打印"welcome

【转】详解Python的装饰器

原文链接:http://python.jobbole.com/86717/ Python中的装饰器是你进入Python大门的一道坎,不管你跨不跨过去它都在那里. 为什么需要装饰器 我们假设你的程序实现了say_hello()和say_goodbye()两个函数. def say_hello(): print "hello!" def say_goodbye(): print "hello!" # bug here if __name__ == '__main__':

Python学习---装饰器的学习1210

装饰器的基础 学习前提: 作用域 + 函数的理解 + 闭包  [学习,理解] 代码编写原则: 对修改开放对扩展开放 装饰器本质上是一个函数,该函数用来处理其他函数,它可以让其他函数在不需要修改代码的前提下增加额外的功能,装饰器的返回值也是一个函数对象. 装饰器的应用:经常用于有切面需求的场景,比如:插入日志.性能测试.事务处理.缓存.权限校验等应用场景.装饰器是解决这类问题的绝佳设计,有了装饰器,我们就可以抽离出大量与函数功能本身无关的雷同代码并继续重用. 装饰器作用:装饰器感觉就像是内部函数的

详解Python的装饰器

Python中的装饰器是你进入Python大门的一道坎,不管你跨不跨过去它都在那里. 为什么需要装饰器 我们假设你的程序实现了say_hello()和say_goodbye()两个函数. def say_hello(): print "hello!" def say_goodbye(): print "hello!" # bug here if __name__ == '__main__': say_hello() say_goodbye() 但是在实际调用中,我们

进阶Python:装饰器 全面详解

进阶Python:装饰器 前言 前段时间我发了一篇讲解Python调试工具PySnooper的文章,在那篇文章开始一部分我简单的介绍了一下装饰器,文章发出之后有几位同学说"终于了解装饰器的用法了",可见有不少同学对装饰器感兴趣.但是那篇文章主要的目的是在介绍PySnooper,所以没有太深入的展开讲解装饰器,于是在这里就详细的介绍一些装饰器的使用. 装饰器是Python中非常重要的一个概念,如果你会Python的基本语法,你可以写出能够跑通的代码,但是如果你想写出高效.简洁的代码,我认

尝试自己的Perl语言的包 TCP协议的再包装起到类似python语言装饰器的效果

#!/usr/bin/perl # Filename: BuildSocketTCP.pm # #   Copyright 2012 Axxeo GmbH #   Licensed under the Apache License, Version 2.0 (the "License"); #   you may not use this file except in compliance with the License. #   You may obtain a copy of t

尝试自己的Perl语言的包 UDP协议的再包装起到类似python语言装饰器的效果

#!/usr/bin/perl # Filename: BuildSocketUDP.pm # #   Copyright 2012 Axxeo GmbH #   Licensed under the Apache License, Version 2.0 (the "License"); #   you may not use this file except in compliance with the License. #   You may obtain a copy of t