4 ms·
Definitely. Indeed decorators are a patch to a shortcoming in Python's ability to define anonymous functions or classes. If you could just say something like:
by euccastro 16y ago
Definitely.
Indeed decorators are a patch to a shortcoming in Python's ability to define anonymous functions or classes. If you could just say something like:
f = memoized(lambda x, y:
... some multiline function)
or
C = coords_or_vectors(class:
... some class definition)
you wouldn't need the kludge:
@memoized
def f(x, y):
...
@coords_or_vectors
class C:
...
Note that I'm not arguing for the syntax in the first two examples, just the ability to do the equivalent without special cases in the normal syntax of the language.
In Scheme you wouldn't need to learn the extra syntax, learn and remember the order in which decorators apply (top to bottom?, inner to outer?).
I was actually very happy when decorators got added to Python. But let's face it, they just give you a way out of a corner Python painted itself into. They're nothing to be too proud of.