Hacker News new | past | comments | ask | show | jobs | submit login

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



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


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.


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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: