DIP guidelines [was: DIP6: Attributes]

Jarrett Billingsley jarrett.billingsley at gmail.com
Sat Aug 1 21:00:02 PDT 2009


On Sat, Aug 1, 2009 at 11:27 PM, Ellery
Newcomer<ellery-newcomer at utulsa.edu> wrote:
> They aren't. Decorators are deep crazy magic. As I recall,
>
> @mydecorator
> def myfunc(argsnjunk):
>    pass
>
> transforms all calls to myfunc to a call to a function you defined
> somewhere named mydecorator, which takes a function and its arguments as
> parameters and returns a function which is expected to call the function
> with its arguments. Then the returned function gets called.

Close, but not quite.  What instead happens is that this gets
transformed into something like:

myfunc = mydecorator()(lambda(argsnjunk): pass)

That is, mydecorator() returns a callable; that callable takes the
function that you're defining as its argument, potentially modifies or
transforms it, then returns either the same function or anything else,
which is finally stored in myfunc.  Calls to myfunc are not
transformed at all; all that has happened is that the decorator has
intercepted the function definition, and might have replaced the
original function with a new one.



More information about the Digitalmars-d mailing list