本文介绍了装饰者的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 i我是python的新手,我有一个关于装饰器如何工作的问题。 我有了解他们如何做他们的魔法,但我想弄明白 什么时候他们这样做... 我有以下简单的例子: #------------------------------------ ----- def author(author_name): def decorator(func): func.author_name = author_name 返回功能 返回装饰器 @author(" some author") def F(): 通过 #print F.author_name #--------------- -------------------------- 我正在使用Eclipse / PyDev,当我从PyDev运行这个片段时br $> 调试器, i看到即使我不调用F()(或引用F.author_name), 装饰器和所有这些东西执行并更新F.autor_name 变量。 怎么样是真的有用吗? 我的意思是,如果我们从命令运行这个.py文件(pythn test.py) 提示, 运行时会做什么,即使我们没有命令执行,只有上面的函数 ? 它将加载所有模块,所有功能,当它看到某些功能正在装饰时,它会开始执行相应的 装饰器? 这将为所有装饰函数做到这一点?即使我们不这样做,但是对这些函数有任何引用? 当我们实际调用它时,不应该调用这段代码吗? 非常感谢任何对此的启发, objectref 解决方案 @decorator def func(): pass 与*完全*完全相同: def func(): pass func = decorator(func ) Python调用装饰器,而不是装饰函数。 额外阅读: http://www.python.org/doc/2.4 .4 / whatsnew / node6.html http://www.python.org/dev/peps/pep-0318 以及教程和语言参考中的相关章节。 < / F> 是的,我知道但是当我调用函数时我认为是这样的, 当运行时只加载模块时... 嗯...好吧......它调用装饰器但是什么时候?它(运行时)加载 .py文件并开始调用它找到的每个装饰器 ,无论实际调用的代码是否存在 装饰的函数?? 我明白Python没有调用decoratated functiond但是它 这样结束... 我将有还有一个lokk thera,谢谢! Python在导入模块时只执行模块体。 class 和def语句导致类和函数对象绑定到 相应的名称。这真的很简单。尝试以下内容 获得更深入的见解: #file foobar.py def bar(f ): 返回''一些文字'' @def吧 def foo(): print" foo" 当您在交互式python会话中导入此模块模块时,您将获得以下内容。 叫做foo Traceback(最近一次调用最后一次): 文件"< ; stdin>",第1行,在? TypeError:''str''对象不可调用 ''一些文字'' 希望有所帮助;) - Soni Bergraj http://www.YouJoy.org/ Hi, i am new to python and i have a question about how decorators areworking.I have understand HOW they do their magic but i am trying to figure out WHEN they do it... I have the following simple example: #-----------------------------------------def author(author_name):def decorator(func):func.author_name = author_namereturn funcreturn decorator @author("some author")def F():pass # print F.author_name#-----------------------------------------I am using Eclipse/PyDev and when i run this snippet from the PyDevdebugger,i see that even though i do not call F() (or reference F.author_name),the decorator and all this stuff is executed and updates F.autor_namevariable. How is this thing really working ??I mean, if we run this .py file (pythn test.py) from the commandprompt,what the runtime will do, even if we do not have ane commands toexecute, only functionsas above ? It will load all the module, all the functions and when it sees thatsome function(s) are decorating, then it will start execute respectivesdecorators ?And this will do it for all decorated functions ?? Even if we do nothave any references to these functions ? Shouldn''t this code called when we actually DO call it ?Thanks a lot for any enlightment on this,objectref 解决方案 @decoratordef func():pass is *exactly* the same thing as: def func():passfunc = decorator(func) Python calls the decorator, not the decorated function. additional reading: http://www.python.org/doc/2.4.4/whatsnew/node6.html http://www.python.org/dev/peps/pep-0318 and the relevant chapters in the tutorial and the language reference. </F> Yes, i know that but i thought that it is so when I call the function,not when the runtime just loads the module... Hmmm...ok...it calls the decorator but when ?? It (the runtime) loadsthe .py file and start to call every decoratorit finds on it, regardless of the existance of code that actually callsthe decorated functions ??I understand thet Python does not call the decoratated functiond but itends up this way... I will have a lokk also thera, thanks! Python simply executes the module body when you import a module. classand def statements result in binding of class and function objects tothe corresponding names. It''s really that simple. Try the following toget a deeper insight: # file foobar.py def bar(f):return ''some text'' @def bardef foo():print "foo"When you import this module module in an interactive python session youwill get he following. called foo Traceback (most recent call last):File "<stdin>", line 1, in ?TypeError: ''str'' object is not callable ''some text'' Hope that helps;)--Soni Bergraj http://www.YouJoy.org/ 这篇关于装饰者的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-11 11:25