decorators question

king kikapu aboudouvas at panafonet.gr
Mon Dec 4 12:56:17 EST 2006


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




More information about the Python-list mailing list