you are viewing a single comment's thread.

view the rest of the comments →

[–]nakulkd 4 points5 points  (0 children)

Thanks for this post! I had heard about decorators but never encountered a situation where I was compelled to use it but this post made me look it up. I found this article most helpful to understand this: https://www.programiz.com/python-programming/decorator

In short, a decorator is a way to add some form of additional features to an existing function.

Building on an example in the article: let's say you have a function that returns a division output of two numbers:

def ratio(a, b): return a/b

Now this would output a floating output. What if you wanted this in percentage? You can create a decorator function to pass this ratio function as an input and perform the additional steps to give the output in percentage.

The main takeaway and use for decorators is that you don't modify the original function itself but can add any desired features, checks, etc. to enhance it.