4 ms·
Understanding Python Decorators
- yason 15y agoI must say that for such a simple concept as decorator, this answer surely used up all the wireframe there ever was allocated for making examples.
- sebkomianos 15y agoI am pretty sure the Python community is always up for better explanations!
- DasIch 15y agoBetter explanations? @foo def bar(): ... Is syntax sugar for def bar(): ... bar = foo(bar) That is all there is to it. It's just syntax sugar to make this particular design pattern more convenient to use. These explanations add more confusion then they dissolve and give off the impression that this is some kind of expert python foo which is clearly isn't.
- ttrtwrtwrtwr 15y ago>That is all there is to it. At it's most basic yes, but passing arguments to both the decorator and the decorated function are the real value of the article. And there are examples, more than I need but they provide different ways of looking at the same thing that different people might find useful.
- urschrei 15y agoWell, OK, but the answer also addresses other cases such as function/method decoration, passing arguments to the decorator itself, and passing arbitrary arguments. I agree that it's a little verbose, and the excessive comments and examples do detract from the clarity of the answer a little, but it's an excellent answer, nevertheless, if only because it leaves no use cases unexplored (excepting class decoration, but that's sufficiently orthogonal, I feel). Your answer isn't wrong, but as an exercise in pedagogy (which is the point of SO, or at least one of its major aims) it leaves a lot to be desired, and wouldn't garner many upvotes. Not everyone understands what the term "syntax sugar" (and really, shouldn't that be 'syntactic sugar'?) means.
- deleted 15y ago[deleted]
- mykhal 15y agoless is more
- peteretep 15y agoStep 1: Huh, that's a pretty cool feature Step 2: I bet I could ... Step 3: Quick check of CPAN Step 4: Zounds. http://search.cpan.org/~erwan/Python-Decorator-0.03/lib/Python/Decorator.pm http://search.cpan.org/~erwan/Python-Decorator-0.03/lib/Pyth...
- grncdr 15y agoThat's pretty cool. I wonder if I could get away with using this at $work... Not to mention, one could alter the implementation to remove the inconsistency people are complaining about in Python.
- buckwild 15y agoI use decorators all of the time. However, I still learned something while reading this comprehensive explanation. Totally worth adding to my bookmarks.
- jackowayed 15y agoAm 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)
- jamii 15y agoIn your own code you can just wrap argument-less decorators in a function. def decorator(): return some_library.decorator_with_no_args @decorator()
- jackowayed 15y agoBut 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.
- kbd 15y agoArmin 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/ http://lucumr.pocoo.org/2011/7/9/python-and-pola/ Discussed on HN too: http://news.ycombinator.com/item?id=2744703 http://news.ycombinator.com/item?id=2744703
- 15y ago
- kingkilr 15y agoNot sure where he gets that Python ships a memoize decorator.
- ddbeck 15y agoWhile 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.lru_cache http://docs.python.org/dev/library/functools.html#functools....
- kingkilr 15y agoI'm aware, there was also almost an LFU cache decorator in there.
- awegawef 15y agoA very good response, glad to see it on SO. It's also worth mentioning that decorators can be classes.
- salem 15y agoPython decorators are extremely useful, especially for neatly adding caching. Style aside, it's extremely cool to be able to add LRU caching, or memcached for a function with one line of code.
- krosaen 15y agoa nice use case for web apps: making sure the current user is logged in, and / or has permission to access a specific resource. removes a ton of boilerplate for redirecting to the login page or returning a 403. tornado makes use of this with the authenticated decorator.
- SoftwareMaven 15y agoHis answer to metaclasses is worth a read, too: http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python/6581949#6581949 http://stackoverflow.com/questions/100003/what-is-a-metaclas...