News for hackers. Stuff that matters.
Re:Joe Stump: Why I switched from PHP to Python by blasdel on Friday September 04, 2009 @09:55PM (#806057)
Decorators are an odd case -- I find that they make code dramatically clearer at the call-site, but that the decorator definitions get hairy fast (and get worse the more life-saving they are). They ended up being one of the best macro systems ever devised (they're applied at compile-time!).
Re:Joe Stump: Why I switched from PHP to Python by njharman on Saturday September 05, 2009 @10:56AM (#806577)
> they're applied at compile-time!

I seriously don't under stand that statement. Decorators are callables that return callables, nothing else, nothing special. Decorator Syntax is just sugar.

Decorators can be applied any time including runtime. I'm not even sure what you mean by "compile time" in regards to Python, import time?

Re:Joe Stump: Why I switched from PHP to Python by blasdel on Saturday September 05, 2009 @06:09PM (#807112)
They're applied at parse-time, and they completely mask the def that they are applied to.

Yes, the function being used as a decorator could just as easily be called at runtime, and that does break the similarity to macros somewhat.

Re:Joe Stump: Why I switched from PHP to Python by njharman on Saturday September 05, 2009 @08:50PM (#807306)
Only decorator syntax happens somewhere around parse-time (which I call import time, but meh).

> completely mask the def that they are applied to.

@decorator reassign to the name of the def. But if there are other refs to that def they do not change and are not "masked". It is no different than assignment. You know that these two are identical, right?

  @foo
  def bar():
    pass
  
  def bar():
    pass
  bar = foo(bar)
Also this is a perfectly valid decorater that doesn't mask the original func

  def deco(func):
    # do something terrible clever here
    return func

decorator syntax != decorators is my main point. But by conflating the two I can begin to understand your statement.