[issue15474] Differentiate decorator and decorator factory in docs
New submission from Éric Araujo <merwok@netwok.org>: Some of the confusion encountered when writing decorators arise from the difference between a decorator (@something) and a decorator factory (@something(args)). It would help to adopt this clearer terminology in our docs: a decorator takes the decorated function as argument and does its business, a decorator factory takes some arguments and returns a decorator which wraps the decorated function. (I think it was Nick who suggested this in a recent-ish mailing list thread.) ---------- assignee: docs@python components: Documentation messages: 166600 nosy: docs@python, eric.araujo, ncoghlan priority: normal severity: normal status: open title: Differentiate decorator and decorator factory in docs versions: Python 2.7, Python 3.2, Python 3.3 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue15474> _______________________________________
Changes by Ezio Melotti <ezio.melotti@gmail.com>: ---------- keywords: +easy nosy: +ezio.melotti stage: -> needs patch type: -> enhancement versions: +Python 3.4 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue15474> _______________________________________
Changes by Ezio Melotti <ezio.melotti@gmail.com>: ---------- nosy: +chris.jerdonek _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue15474> _______________________________________
Ezio Melotti added the comment: The tutorial doesn't seem to mention decorators, do you think this should be covered there? FWIW while explaining decorators I usually use 3 examples: 1) a simple decorator that accepts a function, does something, and returns the same function; 2) a decorator that defines and returns an inner function; 3) a decorator factory, that defines two nested inner functions Not sure if it's necessary to include the first example though. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue15474> _______________________________________
Nick Coghlan added the comment: No, using decorators needs to be in the tutorial, but writing your own is a metaprogramming task that's beyond the tutorial's scope. A HOWTO guide would be appropriate, though. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue15474> _______________________________________
Andrés Delfino <adelfino@gmail.com> added the comment: If deemed appropriate, I can take the task of writing a HOWTO for decorators/decorators factories. ---------- nosy: +adelfino _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue15474> _______________________________________
Fred L. Drake, Jr. <fdrake@gmail.com> added the comment: I like Éric's terminology; giving a concrete name to the concept makes it a lot easier to grasp, and this doesn't require inventing any new component terms. Andrés, if you'd like to tackle this, that's great! I'd be happy to for you to bounce drafts through me if you like. ---------- nosy: +fdrake _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue15474> _______________________________________
Andrés Delfino <adelfino@gmail.com> added the comment: Alright! :D Yes, your help will definitely come in handy, thanks! ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue15474> _______________________________________
Windson Yang <wiwindson@outlook.com> added the comment: Hi, Andrés Delfino. Are you still working on it? ---------- nosy: +Windson Yang _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue15474> _______________________________________
Andrés Delfino <adelfino@gmail.com> added the comment: Hi Windson Yang! Yes, I'm still working on it. I'll have it ready by the end of June. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue15474> _______________________________________
Ezio Melotti <ezio.melotti@gmail.com> added the comment: If you want some early feedback I believe you can already create a PR and mark it as work-in-progress/don't-merge. If you need some material you can find the slides of a talk about decorators that I did at https://www.pycon.it/media/conference/slides/understanding-decorators.pdf Feel free to take from it whatever you need :) ---------- versions: +Python 3.7, Python 3.8, Python 3.9 -Python 2.7, Python 3.2, Python 3.3, Python 3.4 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue15474> _______________________________________
Andrés Delfino <adelfino@gmail.com> added the comment: Great! I'll definitely give it a look. I prefer not to make a PR until everything is in its place. You can check the status though: https://github.com/andresdelfino/cpython/tree/decorators-howto Fred was very helpful with his insight :) ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue15474> _______________________________________
Andrei Daraschenka <dorosch.github.io@yandex.ru> added the comment: Hi, Andrés Delfino. Are you still working on it? I could help with this issues. ---------- nosy: +dorosch _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue15474> _______________________________________
Andrés Delfino <adelfino@gmail.com> added the comment: Hi, no I'm no longer working on this. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue15474> _______________________________________
Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: Thanks Andrés. I'm going to close this one as it has been open for a long time and not attracted interest. Also, in practice this ambiguity doesn't seem to be an issue and people almost universally refer to anything usable with the @-notation as a decorator. Further, it has become common for tooling such as @lru_cache and @dataclass to be usable both with and without arguments, so the distinction no longer makes sense in some cases. ---------- nosy: +rhettinger resolution: -> out of date stage: needs patch -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue15474> _______________________________________
participants (8)
-
Andrei Daraschenka -
Andrés Delfino -
Ezio Melotti -
Fred L. Drake, Jr. -
Nick Coghlan -
Raymond Hettinger -
Windson Yang -
Éric Araujo