from functools import update_wrapper
def debug(func):
def wrapper():
print "[DEBUG]: enter {}()".format(func.__name__)
return func()
return update_wrapper(wrapper,func)#使被修饰的函数名称不会变成wrapper @debug
def say_hello(): #被修饰后,函数的代码会变化,先调用wrapper函数
print "hello!"
print say_hello.__name__
def say_hello2():
print "hello!" say_hello()
'''
Disassembly of module1
''' Disassembly of debug:
16 0 LOAD_CLOSURE 0 (func)
3 BUILD_TUPLE 1
6 LOAD_CONST 1 (<code object wrapper at 0129FB18, file "<module1>", line 16>)
9 MAKE_CLOSURE 0
12 STORE_FAST 1 (wrapper) 19 15 LOAD_GLOBAL 0 (update_wrapper)
18 LOAD_FAST 1 (wrapper)
21 LOAD_DEREF 0 (func)
24 CALL_FUNCTION 2
27 RETURN_VALUE Disassembly of say_hello:
17 0 LOAD_CONST 1 ('[DEBUG]: enter {}()')
3 LOAD_ATTR 0 (format)
6 LOAD_DEREF 0 (func)
9 LOAD_ATTR 1 (__name__)
12 CALL_FUNCTION 1
15 PRINT_ITEM
16 PRINT_NEWLINE 18 17 LOAD_DEREF 0 (func)
20 CALL_FUNCTION 0
23 RETURN_VALUE Disassembly of say_hello2:
26 0 LOAD_CONST 1 ('hello!')
3 PRINT_ITEM
4 PRINT_NEWLINE
5 LOAD_CONST 0 (None)
8 RETURN_VALUE

python 之修饰器的更多相关文章

  1. python中用修饰器进行异常日志记录

    当脚本中需要进行的的相同的异常操作很多的时候,可以用修饰器来简化代码.比如我需要记录抛出的异常: 在log_exception.py文件中, import functools import loggi ...

  2. python函数修饰器(decorator)

    python语言本身具有丰富的功能和表达语法,其中修饰器是一个非常有用的功能.在设计模式中,decorator能够在无需直接使用子类的方式来动态地修正一个函数,类或者类的方法的功能.当你希望在不修改函 ...

  3. python 类修饰器

    1. 修改类函数. 场景: 如果要给一个类的所有方法加上计时,并打印出来.demo如下: # -*- coding:utf-8 -*- import time def time_it(fn): &qu ...

  4. python decorator 修饰器

    decorator 就是给函数加一层皮,好用! 1 from time import ctime 2 3 def deco(func): 4 def wrappedFunc(*args, **kwar ...

  5. Python 函数修饰器

    # 一.用函数修饰函数 #!/usr/bin/python3 def decorate_func(func): def call(*args, **kwargs): print('you have c ...

  6. Python学习-修饰器 - itemgetter的妙用

    下面这篇对装饰器讲的很好,懂了. http://python.jobbole.com/85056/ <简单 12 步理解 Python 装饰器> 使用装饰器非常简单(见步骤10),但是写装 ...

  7. python 通用 修饰器

    import functools def log(option): def dec(func): def swapper(*arg, **karg): functools.update_wrapper ...

  8. Python修饰器的函数式编程

    Python的修饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Design Pattern里的Decorator搞混了,其实这是完全不同的两个东西.虽然好像,他们要干的事都 ...

  9. Python修饰器

    Python的修饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Design Pattern里的Decorator搞混了,其实这是完全不同的两个东西.虽然好像,他们要干的事都 ...

随机推荐

  1. Ubuntu 下超简单的安装指定版本的nodejs

    第一步 指定版本源 执行 curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash - setup_5.x 需要安装的版本号,替换数字就 ...

  2. TCP 三次握爪 四次挥手

    TCP三次握手和四次挥手过程 1.三次握手 (1)三次握手的详述 首先Client端发送连接请求报文,Server段接受连接后回复ACK报文,并为这次连接分配资源.Client端接收到ACK报文后也向 ...

  3. [Ms SQL] 基本創建、修改與刪除

    ##創建 table student, 內涵 id ,name ,tel三種columne,設定id為primary key create table student ( id int primary ...

  4. ansible常用模块入门

    常用模块有以下几个 command copy shell crond yum service setup 1.command模块 ansible george -m command -a " ...

  5. SMD 自动点料机维修

    SMD 自动点料机维修 这个工具是一个好帮手,但是过完年回来发现坏了. 设置了数量不会自动停,按停止键没有反应,一定要按打印键才能停止. 这可愁死我了. 正常情况下开机设置好数量,然后开始点数,点到数 ...

  6. conda使用技巧

    https://www.cnblogs.com/Sinte-Beuve/p/8597429.html https://www.cnblogs.com/xiaotongtt/p/8909844.html ...

  7. httpd2.4.6配置文件解释说明

    本文httpd版本为:2.4.6 ServerRoot 先来看一下httpd.conf配置文件中的ServerRoot默认定义: # cat /etc/httpd/conf/httpd.conf |e ...

  8. Asp.Net : Page.RegisterStartupScript及 不执行的原因

    RegisterStartupScript 把script放置在ASP.NET page的底部,而RegisterClientScriptBlock把script放置在ASP.NET page的顶部 ...

  9. bsdiff差分算法

    bsdiff的基本原理 bsdiff是由Conlin Percival开源的一个优秀的差分算法,而且是跨平台的.在Android系统中所使用的imgdiff本质上就是bsdiff. bsdiff的依据 ...

  10. 为什么选择Go语言 GO语言都能做什么产品

    Go语言,又称Golang,是Google开发的一款静态强类型.编译型.并发型,并具有垃圾回收机制的编程语言,它的运行速度非常之快,同时还有如下特性:具有一流的标准库.无继承关系.支持多核:同时它还有 ...