Python Tutorial: Decorators With Arguments

///Python Tutorial: Decorators With Arguments

Python Tutorial: Decorators With Arguments

FavoriteLoadingAdd to favorites

In this Python tutorial, we will be learning how to create decorators with parameters that accept arguments. This was a highly requested video in response to my original decorator tutorial video. Accepting arguments allows us to add even more functionality to our decorators. You will see this throughout many frameworks and libraries, so it’s a good skill to know. Let’s get started…

Original Decorator Tutorial:

The code from this video can be found at:

✅ Support My Channel Through Patreon:

✅ Become a Channel Member:

✅ One-Time Contribution Through PayPal:

✅ Cryptocurrency Donations:
Bitcoin Wallet – 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet – 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet – MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey’s Public Amazon Wishlist

✅ Equipment I Use and Books I Recommend:

▶️ You Can Find Me On:
My Website –
My Second Channel –
Facebook –
Twitter –
Instagram –

#Python

source

By |2019-10-30T23:41:34+00:00October 30th, 2019|Python Video Tutorials|24 Comments

24 Comments

  1. Bhanu Bandaru October 30, 2019 at 11:41 pm - Reply

    Corey by far gives the best explanations with good concise introductions of relevant prerequisite concepts.
    But this video was tough to follow. I feel this one is missing an intro to how the python interpreter will deal with a decorator with arguments.
    In the previous video, the explanation is very clear:

    @decorator_func
    myfunc():

    is equivalent to myfuc = decorator_func(my_func)

    Such an explanation is lacking here.

  2. italonesi October 30, 2019 at 11:41 pm - Reply

    That was the most beautiful example and definition of @decorators I have seen

  3. John Doe October 30, 2019 at 11:41 pm - Reply

    Can I please ask why you return result from your decorator function? Is it because we cannot predict which value the decorated function will return, in this case, it is a None, but it might have been something else.

  4. Math Jon October 30, 2019 at 11:41 pm - Reply

    you deserve eevry like i do

  5. Andreas Miles October 30, 2019 at 11:41 pm - Reply

    Thanks, as always great quality content. I like the fact other people too are on "first name terms" with you despite the one way channel of communication.

  6. Sanjay Chamlagain October 30, 2019 at 11:41 pm - Reply

    Can anyone help me to code this functionality using class?????

  7. nihat nihat October 30, 2019 at 11:41 pm - Reply

    hey there, great playlist btw. Really learning key points about Python, thanks to you. And most importantly I am enjoying while watching your tutorials. Here is my question. Let's say if I would want to put prefix "TESTING" for one user and put "LOG" for another user. I could not achieve by myself. The solution seemed to be duplication functions but I know it is nonesense 🙂

  8. Nasa Lee October 30, 2019 at 11:41 pm - Reply

    I confused about line 9 'return result' and the final output.
    and I thought the output should be like this:
    "Executed Before display_info"
    "Executed After display_info "
    "display_info ran with arguments (John, 25)",
    So I put all the codes in visualize python website http://www.pythontutor.com. When on step 15, the "Return value is None", then I remove the line 9. this codes has the same output as the yours.
    Removing this line('return result') is right or wrong?

  9. Victor Tuekam October 30, 2019 at 11:41 pm - Reply

    thank you

  10. Khushboo Tiwari October 30, 2019 at 11:41 pm - Reply

    hi @corey can you please describe implementation of design patterns (singleton abstract factory etc) in python.
    thanks in anticipation.

  11. Mudith October 30, 2019 at 11:41 pm - Reply

    I usually use ad block on YouTube but before clicking on Corey's videos I always make sure to turn it off. Thanks so much Corey 🙂

  12. Krystian Raj October 30, 2019 at 11:41 pm - Reply

    Simple and perfect! Thank you!

  13. Mohamed Eldesouki October 30, 2019 at 11:41 pm - Reply

    I have taken a course in university a bout programming concepts it was dry and hard to get but with you I watch your videos in my spare time as it is something fun… thanks Corey

  14. Hu Hao Yang October 30, 2019 at 11:41 pm - Reply

    Thank you so much and great job explaining this technique!!! I think my head exploded after watching this video for the first time. But I watched it the second time and then I was able to get a handle on this concept.

  15. para0rmal 1d345 October 30, 2019 at 11:41 pm - Reply

    Thank you!

  16. man1army October 30, 2019 at 11:41 pm - Reply

    High quality as always! Thank you.

  17. Antonios Skourtis October 30, 2019 at 11:41 pm - Reply

    Hi Corey! I have a question I hope you can answer. In your wrapper function you declare "result = original_function(*args, **kwargs)" and then a bit further down you return result. Is this really necassary since the original_function is actually printing something? My question is really when you call original_function(*args, **kwargs) isn't the original function executing its print statement directly? I hope you understand what I mean =) BR //Antonios

  18. Chuankai Zhou October 30, 2019 at 11:41 pm - Reply

    Hi Corey, can you explain a little bit on how the prefix_decorator() works in this example? I was trying to understand it in the way you described in your previous decorator video by converting it into: display_info = prefix_decorator('LOG:', display_info), but the prefix_decorator() only take one arg 'prefix', so I don't understand how both the 'LOG:' and the display_info() was passed into the decorator function?

  19. Zwork101 October 30, 2019 at 11:41 pm - Reply

    Every video you make is always so high quality! Do you think you could make a threading tutorial?

  20. kartik sibal October 30, 2019 at 11:41 pm - Reply

    Amazing videos corey!
    It'd also be amazing if you maybe could add tutorials for code testing in python.

    Cheers!

  21. Khalil Al Hooti October 30, 2019 at 11:41 pm - Reply

    Do you have any tutorials on python iterators and generators!!. Thanks for these great videos

  22. Khalil Al Hooti October 30, 2019 at 11:41 pm - Reply

    thank you very much for the tutorials, please keep it up and bring more python tutorials specially in science domain. Can you please make some tutorial into abstract classes and meta-classes.

  23. Robin Siwach October 30, 2019 at 11:41 pm - Reply

    Amazing tuts. Hey please make a complete series on data structures implemented in python that covers some advance data structures too like they have done in book by Michael T Goodrich

  24. Sumit Singhal October 30, 2019 at 11:41 pm - Reply

    Hi Corey, Again a wonderful tutorial. It would be a great help if you add videos for descriptors and metaclasses too.

Leave A Comment

*