confusion with decorators

Duncan Booth duncan.booth at invalid.invalid
Mon Feb 4 05:19:32 EST 2013


Dave Angel <davea at davea.name> wrote:

> The decorator function will execute while *compiling* the class A, and 
> the one in class B is unreferenced.

No, the decorator function is called when *executing* the class body of A. 
Compilation could have happened weeks earlier.

It really does make it a lot easier to understand this sort of issue if you 
remember that 'class' and 'def' are simply executable statements: the body 
of 'class' executes as part of the class definition whenever normal 
execution reaches the 'class' statement, the body of 'def' doesn't execute 
until the function is called (but default arguments are evaluated when the 
'def' is executed).

In both cases however the code is fully compiled whenever the module is 
compiled: that could be when the module is imported or if it is __main__ 
when the script executes, but after the first run of the program all of the 
modules (nto the script) will have been compiled once and don't compile 
again until the source changes.

-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list