【转载】 python修饰符@
@符号在python语言中具有特殊含义,用来作为修饰符使用, @修饰符有点像函数指针,python解释器发现执行的时候如果碰到@修饰的函数,首先就解析它,找到它对应的函数进行调用,并且会把@修饰下面一行的函数作为一个函数指针传入它对应的函数。
参考下面的代码:
def spamrun(fn):
def sayspam(*args):
a,b =args
c,d = a*5, b*5
print c,d
return fn(c,d)
print 'note!'
return sayspam
@spamrun
def useful(a,b):
print a*2, b*2
if __name__== "__main__":
#useful(2,3)
print 'nothing'
运行结果:
note!
nothing
输出了“note!",spamrun方法已经运行过了。说明:
@spamrun是一个statement,会将useful当作参数传入来执行spamrun方法。
如果将上段代码中的useful(2,3)去年注释,输出为:
note!
10 15
20 30
nothing
这个输出结果说明:
spamrun方法运行完毕后,usefull方法已经指向sayspam方法了,而fn此时指向的是原来usefull的方法实体。
转载来自:http://blog.csdn.net/thumb3344/article/details/5645124
【转载】 python修饰符@的更多相关文章
- python 修饰符(转载)
首先一个修饰符的实例: #!/usr/bin/env python def run(fn): def do_hello(): print "begin..." fn() print ...
- python——修饰符
修饰符基础--闭包 什么是闭包呢?标准的概念大家可以看wikipedia上的解释 举个例子: def do_add(base): def add(increase): return base + in ...
- Python 修饰符, 装饰符
1, 看到@时候, 程序已经开始执行了. 所以@实际上是立即执行的 2, @后面的跟着函数名, 该函数(f1)是之前定义过的. 再后面跟着一个函数(f2), f2是f1的入口. 那么执行顺序是, ...
- Python 修饰符
def hello(fn): def wrapper(): print "hello" fn() print "goodb ...
- python @修饰符的几种用法
http://www.360doc.com/content/17/0715/16/10408243_671545922.shtml http://www.cnblogs.com/Egbertbaron ...
- Python 修饰符@用法
def funA(desA): print("It's funA") def funB(desB): print(desB( )) print("It's funB&qu ...
- 【转载】Python 描述符简介
来源:Alex Starostin 链接:www.ibm.com/developerworks/cn/opensource/os-pythondescriptors/ 关于Python@修饰符的文章可 ...
- Python 中的@修饰符作用
在Python 2.4以上的的函数中偶尔会看到函数定义的上一行有@functionName的修饰,这一下这个语法细节,其实这有点像C语言带参数的宏操作,解释器读到这样的修饰之后,会先解析@后的内容,直 ...
- @修饰符--python中的装饰器
装饰器模式可以在不影响其他对象的情况下,以动态.透明的方式给单个对象添加职责,也能够处理那些可以撤销的职责.经常用于日志记录.性能测试等场合. 想象一下这个很常见的场景,你写了一个方法只提供给以登陆的 ...
随机推荐
- git 教程(1)--安装git
在Linux上安装Git 首先,你可以试着输入git,看看系统有没有安装Git: gi 如果你碰巧用Debian或Ubuntu Linux,通过一条sudo apt-get install git就可 ...
- C# 属性和索引
//用索引取一个记录中的各项 using system; class IndexerRecord{ private string[] data= new string [6]; private str ...
- java代码生成二维码以及解析二维码
package com.test; import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedI ...
- note:获取字符输入的一些函数
总是弄混,所以总结一下: getline() // 接受一个字符串,可以接收空格并输出,需包含“#include<string>” #include<iostream> ...
- PyQt4自定义事件
listview控件与updateText 相关联 self.listview.updateText.connect(self.viewlist) updateText = QtCore.pyqt ...
- centos 6.5 u盘 安装问题 :vesamenu.c32: Not a COM32R image
大致可以参考这里:http://www.computerandyou.net/2012/03/how-to-solve-vesamenu-c32-not-a-com32r-image-error-in ...
- iOS 深入理解UINavigationController 和 UIViewController 之间的关系
创建三个类 BasicViewController : UIViewController SecondViewController : UIViewController ThirdViewContro ...
- 《Head First Servlet JSP》学习笔记二
一. 二. 三. 四. 五. 六. 七. 八. 九. 十. 十一. 十二.
- js日期、月份:日期加一天等
// 日期,在原有日期基础上,增加days天数,默认增加1天 function addDate(date, days) { if (days == undefined || days == '') { ...
- PHP javascript cookie
2015-07-30 16:54:58 ................................cao!!!! 汉字, 邮箱的@符号 容易出错 PHP setcookie 的时候, 不要url ...