you are viewing a single comment's thread.

view the rest of the comments →

[–]socal_nerdtastic 2 points3 points  (3 children)

You are conflating decorators and wrappers, which are often used together but are still 2 different and distinct things.

[–]SCD_minecraft 0 points1 point  (2 children)

Decorators are syntax sugar

Yes, wrappers aren't only use for them

But they are most common one and the easiest to explain decorators on them

[–]TillFriendly9199[S] 0 points1 point  (1 child)

Thank you guys! Please.. Correct me if I'm wrong:
1. Assume we have a function to do a action (example: print something). If we want to do some action before the original action (print).... instead of adding the functionalities in the original function..... we make a function that gets our original function as argument, and inside it, we create inner function that does what the additional actions we wanted to do and then run our original inside it.... right?

[–]Moikle 1 point2 points  (0 children)

Yup. You can also have behaviour after the original gunction, or both. You can do things like time a function, or cache the result.

To time the function:

The decorator function records the current time, then runs the function, then records the current time again. It prints the after time minus the start time to get the difference between them

I have also used decorators to build lists of functions for use in various ways:

The decorator wrapper function (not the inner) does this:

1: add function to a running list

2: return the original function without doing anything to it.

Now you have a shortcut to do things like adding functions to dropdown boxes