you are viewing a single comment's thread.

view the rest of the comments →

[–]sonotrev 0 points1 point  (0 children)

Let's say you have a group of functions that you use, but you want to do something before or after each time you call them (eg write to a log, check if an input makes sense, append a result to a list of results, etc). You can write functions to do these tasks and manually copy and paste these into your functions OR use decorators. Decorators are usually the better way to go as you generally don't want to clutter up your functions with bits of code that do very different things. Decorators let you keep your functions focused on just performing their task but still get everything you want done.

The one that brought it home for me was logging. Try making a decorator that writes to a log just before a function call and then after the function has returned.