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)
Understanding Python Decorators
11–20 of 28 posts
Re: Understanding Python Decorators
#12Re: Understanding Python Decorators
#13Not sure where he gets that Python ships a memoize decorator.
http://docs.python.org/dev/library/functools.html#functools....
Re: Understanding Python Decorators
#14Not sure where he gets that Python ships a memoize decorator.
While not actually called memoize, Python 3.2 ships with the lru_cache decorator in the standard library module functools. http://docs.python.org/dev/library/functools.html#functools....
Re: Understanding Python Decorators
#15Am 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 i…
def decorator():
return some_library.decorator_with_no_args
@decorator()Re: Understanding Python Decorators
#16Am 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 i…
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
#17Am 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 i…
> 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:
Re: Understanding Python Decorators
#18Re: Understanding Python Decorators
#19Am 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 i…
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
#20Am 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 i…