装饰者的问题 [英] decorators question

查看:70
本文介绍了装饰者的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




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

解决方案

king kikapu写道:


它将加载所有模块,所有功能以及何时看到

某些功能正在装饰,那么它将开始执行各自的

装饰器?



@decorator

def func():

pass


与*完全*完全相同:


def func():

pass

func = decorator(func )


这将为所有装饰函数做到这一点?即使我们没有

有这些功能的参考吗?



Python调用装饰器,而不是装饰函数。


额外阅读:

< a rel =nofollowhref =http://www.python.org/doc/2.4.4/whatsnew/node6.html\"target =_ blank> http://www.python.org/doc/2.4 .4 / whatsnew / node6.html
http://www.python.org/dev/peps/pep-0318


以及教程和语言参考中的相关章节。


< / F>


def func():


通过


与*完全相同*:


def func():

通过

func = decorator(func)



是的,我知道但是当我调用函数时我认为是这样的,

当运行时只加载模块时...


> Python调用装饰器,而不是装饰函数。



嗯...好吧......它调用装饰器但是什么时候?它(运行时)加载

.py文件并开始调用它找到的每个装饰器

,无论实际调用的代码是否存在

装饰的函数??

我明白Python没有调用decoratated functiond但是它

这样结束...


>

额外阅读:

http://www.python.org/doc/2.4.4/whatsnew/node6.html
http://www.python.org/dev/peps/ pep-0318


以及教程和语言参考中的相关章节。



我将有还有一个lokk thera,谢谢!


< / F>


嗯......好吧......它调用装饰器但是什么时候?它(运行时)加载


.py文件并开始调用它找到的每个装饰器

,无论是否存在实际调用的代码

装饰函数??

我理解Python不会调用decoratated函数但它会以这种方式结束
。 ..



Python在导入模块时只执行模块体。 class

和def语句导致类和函数对象绑定到

相应的名称。这真的很简单。尝试以下内容

获得更深入的见解:


#file foobar.py


def bar(f ):

返回''一些文字''


@def吧

def foo():

print" foo"

当您在交互式python会话中导入此模块模块时,您将获得以下内容。


>>来自foobar import *



叫做foo


>> bar()#这将失败因为bar不是函数



Traceback(最近一次调用最后一次):

文件"< ; stdin>",第1行,在?

TypeError:''str''对象不可调用


>> bar



''一些文字''


希望有所帮助;)

-

Soni Bergraj
http://www.YouJoy.org/


Hi,

i am new to python and i have a question about how decorators are
working.
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_name
return func
return decorator

@author("some author")
def F():
pass

# print F.author_name
#-----------------------------------------
I am using Eclipse/PyDev and when i run this snippet from the PyDev
debugger,
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_name
variable.

How is this thing really working ??
I mean, if we run this .py file (pythn test.py) from the command
prompt,
what the runtime will do, even if we do not have ane commands to
execute, only functions
as above ?

It will load all the module, all the functions and when it sees that
some function(s) are decorating, then it will start execute respectives
decorators ?
And this will do it for all decorated functions ?? Even if we do not
have 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

解决方案

king kikapu wrote:

It will load all the module, all the functions and when it sees that
some function(s) are decorating, then it will start execute respectives
decorators ?

@decorator
def func():
pass

is *exactly* the same thing as:

def func():
pass
func = decorator(func)

And this will do it for all decorated functions ?? Even if we do not
have any references to these functions ?

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>


def func():

pass

is *exactly* the same thing as:

def func():
pass
func = decorator(func)

Yes, i know that but i thought that it is so when I call the function,
not when the runtime just loads the module...

>Python calls the decorator, not the decorated function.


Hmmm...ok...it calls the decorator but when ?? It (the runtime) loads
the .py file and start to call every decorator
it finds on it, regardless of the existance of code that actually calls
the decorated functions ??
I understand thet Python does not call the decoratated functiond but it
ends up this way...

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

I will have a lokk also thera, thanks!

</F>


Hmmm...ok...it calls the decorator but when ?? It (the runtime) loads

the .py file and start to call every decorator
it finds on it, regardless of the existance of code that actually calls
the decorated functions ??
I understand thet Python does not call the decoratated functiond but it
ends up this way...

Python simply executes the module body when you import a module. class
and def statements result in binding of class and function objects to
the corresponding names. It''s really that simple. Try the following to
get a deeper insight:

# file foobar.py

def bar(f):
return ''some text''

@def bar
def foo():
print "foo"
When you import this module module in an interactive python session you
will get he following.

>>from foobar import *

called foo

>>bar() # this will fail because bar is not a function

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: ''str'' object is not callable

>>bar

''some text''

Hope that helps;)
--
Soni Bergraj
http://www.YouJoy.org/


这篇关于装饰者的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆