5 ms·
You might want to look into class-based decorators, they take some of the pain away.
by Qantourisc 13y ago
You might want to look into class-based decorators, they take some of the pain away.
- Suor 13y agoThis looks like even more boilerplate.
- ekimekim 13y agoArguably, it can make things easier to read in cases where you want "static" variables, eg. a decorator that synchronises a function with a lock. The lock shouldn't be passed in by the user, nor should it be created in an individual invocation of the decorated function, so having a class to contain the state is useful.
- jessaustin 13y agoThat's a great situation in which to use a class: state you don't want to pass in every time it's used. It doesn't seem to connect to the idea of a decorator, though. Rather than decorating the function, why not just make it a method of the state-managing class? Or if it shouldn't be so closely coupled, why not just pass it the result of invoking a different method of the class? I don't imply that there isn't a good example of a decorator that should be a class. I just don't think we've seen one on this thread yet.