When to use decorators in Python » комментарииhttps://softwaremaniacs.org/blog/2012/07/09/when-to-use-decorators/2012-07-31T05:46:36-07:00Иван Сагалаев о программировании и веб-разработкеhttp://softwaremaniacs.org/media/sm_org/style/photo.jpgSebastian Rockefeller на "When to use decorators in Python"
2012-07-31T05:46:36-07:00Sebastian Rockefellerhttps://softwaremaniacs.org/blog/2012/07/09/when-to-use-decorators/#comment-148835Did you know that knowledge base software from Website Scripts is developed using decorators with Python
<p>Did you know that <a href="http://www.web-site-scripts.com/knowledge-management/overview.html">knowledge base software</a> from Website Scripts is developed using decorators with PythonseriyPS на "When to use decorators in Python"
2012-07-13T15:43:25.336347-07:00seriyPShttps://softwaremaniacs.org/blog/2012/07/09/when-to-use-decorators/#comment-142602I think this article was inspired by that comment: http://vorushin.ru/blog/26-decorators-python/#comment-248848219 When I first time read this comment I remember it for a long time)
<p>I think this article was inspired by that comment: <a href="http://vorushin.ru/blog/26-decorators-python/#comment-248848219">http://vorushin.ru/blog/26-decorators-python/#comment-248848219</a></p>
<p>When I first time read this comment I remember it for a long time)Ivan Sagalaev на "When to use decorators in Python"
2012-07-10T17:07:14.779093-07:00Ivan Sagalaevhttps://softwaremaniacs.org/blog/2012/07/09/when-to-use-decorators/#comment-141753piranha: I believe they are not inherently bad, they are bad only when you do crappy stuff with them. This is true about any rule of thumb: you can break it if you understand implications. But these "rules" do help in the process of this judgment anyway.
<p><em>piranha</em>:</p>
<blockquote>
<p>I believe they are not inherently bad, they are bad only when you do crappy stuff with them.</p>
</blockquote>
<p>This is true about any rule of thumb: you can break it if you understand implications. But these "rules" do help in the process of this judgment anyway.Andrey Popp на "When to use decorators in Python"
2012-07-10T12:28:13.230598-07:00Andrey Popphttps://softwaremaniacs.org/blog/2012/07/09/when-to-use-decorators/#comment-141705There's useful lib for doing all fancy things in a clean way with decorators - venusian http://docs.pylonsproject.org/projects/venusian/en/latest/ by @chrism - it simplifies creation of decorators which only attach some callback (which can be deferred by the way) to function thus not modifying original function at all.
<p>There's useful lib for doing all fancy things in a clean way with decorators - venusian <a href="http://docs.pylonsproject.org/projects/venusian/en/latest/">http://docs.pylonsproject.org/projects/venusian/en/latest/</a> by @chrism - it simplifies creation of decorators which only attach some callback (which can be deferred by the way) to function thus not modifying original function at all.ncoghlan_dev на "When to use decorators in Python"
2012-07-10T02:40:53-07:00ncoghlan_devhttps://softwaremaniacs.org/blog/2012/07/09/when-to-use-decorators/#comment-141574In 3.2+,functools.wraps adds a "wrapped" attribute to bypass the wrapping decorator (this was driven by the introduction of lru_cache). More generally, while it needs to be used with restraint, it's entirely appropriate for decorators to be semantically significant. Consider @classmethod, @staticmethod, @property and @contextmanager.
<p>In 3.2+,functools.wraps adds a "<strong>wrapped</strong>" attribute to bypass the wrapping decorator (this was driven by the introduction of lru_cache).</p>
<p>More generally, while it needs to be used with restraint, it's entirely appropriate for decorators to be semantically significant. Consider @classmethod, @staticmethod, @property and @contextmanager.piranha на "When to use decorators in Python"
2012-07-10T02:18:29.913211-07:00piranhahttps://softwaremaniacs.org/blog/2012/07/09/when-to-use-decorators/#comment-141569how to use the decorated function by looking only at its code (or docs) without hunting for the definition of the decorator. Hey-hey, how about docs for decorator? ;) In any case, your example makes sense as an example of abusing decorator syntax and crappy API at the same time....
<blockquote>
<p>how to use the decorated function by looking only at its code (or docs) without hunting for the definition of the decorator.</p>
</blockquote>
<p>Hey-hey, how about docs for decorator? ;)</p>
<p>In any case, your example makes sense as an example of abusing decorator syntax <em>and</em> crappy API at the same time. But then you could make a really bad hierarchy of classes and abuse them to death with metaclasses. And it could be nice and useful. Or terminally bad. Does it mean we shouldn't write and use metaclasses which change semantics of classes completely? </p>
<p>I.e. Django forms or models - you define properties, but then they are totally gone from your class. But then suddenly they are here on your instance. Isn't this like a decorator, which changes semantics of a function? Isn't then decorators changing semantics justificable?</p>
<p>I believe they are not inherently bad, they are bad only when you do crappy stuff with them. Which is always bad, but it's not like there are simple rules to follow to make people not do crappy things.Ivan Sagalaev на "When to use decorators in Python"
2012-07-09T23:36:30.169706-07:00Ivan Sagalaevhttps://softwaremaniacs.org/blog/2012/07/09/when-to-use-decorators/#comment-141529OK, got it. This is exactly what I meant in my side note :-).
<p>OK, got it. This is exactly what I meant in my side note :-).dm на "When to use decorators in Python"
2012-07-09T22:46:14.710414-07:00dmhttps://softwaremaniacs.org/blog/2012/07/09/when-to-use-decorators/#comment-141517Oh, I wasn't supporting Michael's words. I was trying to show that all wraps does is preserving __module__, __name__ and __doc__ as well as __dict__. None of those give you an access to the original function, so the answer to your question is you don't, as far as I can...
<p>Oh, I wasn't supporting Michael's words. I was trying to show that all <code>wraps</code> does is preserving <code>__module__</code>, <code>__name__</code> and <code>__doc__</code> as well as <code>__dict__</code>. None of those give you an access to the original function, so the answer to your question is you don't, as far as I can see.Ivan Sagalaev на "When to use decorators in Python"
2012-07-09T22:17:59.885018-07:00Ivan Sagalaevhttps://softwaremaniacs.org/blog/2012/07/09/when-to-use-decorators/#comment-141508The source didn't make it clear about what you (and Michael) mean :-). I still suspect we're talking about different things here. I was saying about accessing the original function from the surrounding namespace, Michael was saying about preserving the namespace of the function. In other words, given the example...
<p>The source didn't make it clear about what you (and Michael) mean :-). I still suspect we're talking about different things here. I was saying about accessing the original function from the surrounding namespace, Michael was saying about preserving the namespace <em>of</em> the function.</p>
<p>In other words, given the example in my previous comment, how to access the original <code>func()</code> that when called would not stick the word "decorated" to the result?dm на "When to use decorators in Python"
2012-07-09T21:38:56.135447-07:00dmhttps://softwaremaniacs.org/blog/2012/07/09/when-to-use-decorators/#comment-141503You can check the source code for wraps here to get the idea of what it does: http://hg.python.org/cpython/file/4f891f44ec15/Lib/functools.py#l12
<p>You can check the source code for wraps here to get the idea of what it does:</p>
<p><a href="http://hg.python.org/cpython/file/4f891f44ec15/Lib/functools.py#l12">http://hg.python.org/cpython/file/4f891f44ec15/Lib/functools.py#l12</a>Ivan Sagalaev на "When to use decorators in Python"
2012-07-09T20:43:58.012297-07:00Ivan Sagalaevhttps://softwaremaniacs.org/blog/2012/07/09/when-to-use-decorators/#comment-141492Or wait… I tried this: def decorator(f): @wraps(f) def wrapper(value): return 'decorated %s' % f(value) return wrapper @decorator def func(value): return 'process %s' % value … and func.func doesn't exist. What did you mean then?
<p>Or wait… I tried this:</p>
<pre><code>def decorator(f):
@wraps(f)
def wrapper(value):
return 'decorated %s' % f(value)
return wrapper
@decorator
def func(value):
return 'process %s' % value
</code></pre>
<p>… and <code>func.func</code> doesn't exist. What did you mean then?Ivan Sagalaev на "When to use decorators in Python"
2012-07-09T20:38:50.269956-07:00Ivan Sagalaevhttps://softwaremaniacs.org/blog/2012/07/09/when-to-use-decorators/#comment-141491Michael, thanks, I didn't know wraps keeps the original function in the .func attribute of the decorator.
<p>Michael, thanks, I didn't know <code>wraps</code> keeps the original function in the <code>.func</code> attribute of the decorator.Michael Warkentin на "When to use decorators in Python"
2012-07-09T19:45:12.892357-07:00Michael Warkentinhttps://softwaremaniacs.org/blog/2012/07/09/when-to-use-decorators/#comment-141477re: your sidenote - you can use functools.wraps (http://docs.python.org/library/functools.html#functools.wraps) in order to preserve the decorated function namespace.
<p>re: your sidenote - you can use functools.wraps (<a href="http://docs.python.org/library/functools.html#functools.wraps">http://docs.python.org/library/functools.html#functools.wraps</a>) in order to preserve the decorated function namespace.