News for hackers. Stuff that matters.
Re:Understanding Python Decorators by jackowayed on Sunday August 07, 2011 @04:52PM (#2857685)
Am I the only one who thinks it's a massive flaw that decorators that take args and decorators that don't are inherently different? That extra level of anonymous functions feels awful, and it's really confusing because a decorator that takes no args is @decorator whereas one that takes optional args is @decorator() if you just go with the defaults, and there is no way to make them both work.

Also, I wish they would integrate some of the stuff from this answer into the documentation, as it took me almost an hour to figure out how decorators that take arguments work and to discover that @decorator and @decorator() are irrevocably different. The documentation I found on decorators that take args was very brief. (I can't remember if I found any mention at all in the actual Python docs. I ended up learning it from mediocre, old blog posts)

Re:Understanding Python Decorators by kbd on Sunday August 07, 2011 @06:00PM (#2857867)
Armin Ronacher (prominent Python developer) included a critique of decorators in a post about a month ago making precisely your point:

> Decorators are somewhat of a pain because there is a difference between @foo and @foo(). If they were declared in a way that the former means the latter we would all be much happier now. Every time I want to introduce a parameter to a previously parameterless decorator makes me want to hit myself and the author of the decorator PEP with a stick.

http://lucumr.pocoo.org/2011/7/9/python-and-pola/

Discussed on HN too:

http://news.ycombinator.com/item?id=2744703

Re:Understanding Python Decorators by omaranto on Sunday August 07, 2011 @06:50PM (#2858031)
It only feels wrong because you think of them as decorators without arguments and decorators with arguments: they really are decorators stored in variables and decorators computed by function calls. :)
Re:Understanding Python Decorators by rbonvall on Monday August 08, 2011 @12:13AM (#2858737)
That's exactly how I see it. One could have also, for example, lists of decorators,

    @a[1]
    def f(x):
        ...
Re:Understanding Python Decorators by SoftwareMaven on Monday August 08, 2011 @12:50AM (#2858834)
I do. The people who don't are thinking from the implementor perspective instead of the user perspective.

I had a permission checking decorator that I allowed both syntaxes (the default permission check and a mre fine-grained check). The flaw makes for an ugly implementation.

Re:Understanding Python Decorators by andrewcooke on Sunday August 07, 2011 @06:43PM (#2858014)
one workaround is to make your decorators require keyword arguments (which often makes good sense from a documentation POV). you can then add a first keyword argument as gatekeeper, with default value None, and check whether it has been defined. if so, the decorator was used incorrectly.

you can see what i mean here - http://code.google.com/p/lepl/source/browse/src/lepl/matcher...

this doesn't fix the problem completely, of course, but it does mean that a user that types @foo instead of @foo() gets a helpful warning explaining exactly what they have done wrong.

Re:Understanding Python Decorators by jamii on Sunday August 07, 2011 @05:51PM (#2857833)
In your own code you can just wrap argument-less decorators in a function.

    def decorator():
      return some_library.decorator_with_no_args

    @decorator()
Re:Understanding Python Decorators by jackowayed on Sunday August 07, 2011 @05:52PM (#2857841)
But then @decorator doesn't work. I guess if I did that for every single decorator I use, I would at least have consistency. But that's not a very satisfying solution even ignoring the fact that now I have to wrap every zero-arg decorator I want to use.