4 ms·
I don't know many languages, do decorators show up in any others? I _love_ them in Python.
by drcongo 5y ago
I don't know many languages, do decorators show up in any others? I _love_ them in Python.
- BiteCode_dev 5y agoYes. In fact, E.G: C# and JS have them, although they don't have exactly the same features. edit, sorry I've meant generators. Incidentally, it's also true for js decorators.
- k__ 5y agoHow do Python generators differ from JavaScript generators?
- BiteCode_dev 5y agoYield from comes to mind.
- k__ 5y agoIsn't JS yield* the same as yield from in Python
- BiteCode_dev 5y agoUnless I'm mistaken, it propagate pulling, but not pushing with send(). I haven't checked though.
- goatlover 5y agoDecorators are still a pending proposal in ECMAScript, so they would only be available to transpilers. That makes them non-standard until the proposal is approved, where the implementation could change.
- useerup 5y agoIn C#: static IEnumerable<int> Fibonacci() { int first = 0; int second = 1; yield return first; yield return second; while (true) { (first, second) = (second, second + first); yield return second; } }
- whoisthemachine 5y agoInteresting use of tuples I had never considered before, a lot of state is packed in that `(first, second)...` block! Pretty fun though.
- b3morales 5y agoEmacs Lisp* has a system called "advice" that's maybe a bit clumsier than Python's, but is also more powerful. The relationship between decorator and decorated [is configurable][0]. For example, the decorated function could be called conditionally on the result of applying the decorator to the arguments, something like: def _combined(args): return decorator(args) and original(args) It can also be applied retroactively, not just at the decorated function's definition. This is extremely handy in the context of configuring your text editor, but I admit I wouldn't like it in a program I was working on with other people. (Basically a form of monkeypatching.) * Granted not really used for general-purpose programming like Python. [0]:https://www.gnu.org/software/emacs/manual/html_node/elisp/Advice-Combinators.html https://www.gnu.org/software/emacs/manual/html_node/elisp/Ad...
- creamytaco 5y agoEmacs Lisp has macros which are a lot more elegant (not to mention powerful) than Python decorators which comparatively, are nothing more than a kludge.
- krick 5y agoDepends on what you mean. I often hear claims that you can use decorators in language X, which are formally true, since decorator is just a function accepting and returning another function, so any language that has anonymous functions (which is a lot of languages by now) "has decorators". But for me this is kind of false marketing, because there's nothing impressive about that, and the killer feature of Python's decorators is the syntactic sugar, which makes them so easy to use. And in this sense not so many languages "have decorators". You can do something similar with annotations in PHP, but I've never actually done this in a real project, because that feels like abusing the language.