Python Tutorial: Decorators – Dynamically Alter The Functionality Of Your Functions

///Python Tutorial: Decorators – Dynamically Alter The Functionality Of Your Functions

Python Tutorial: Decorators – Dynamically Alter The Functionality Of Your Functions

FavoriteLoadingAdd to favorites

In this Python tutorial, we will be learning about decorators. Decorators are a way to dynamically alter the functionality of your functions. So for example, if you wanted to log information when a function is run, you could use a decorator to add this functionality without modifying the source code of your original function. So let’s take a look at how these decorators work and a few ways in which we can use them. Let’s get started.

First-Class Functions Video:
Closures Video:

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 |2020-01-19T01:17:22+00:00January 19th, 2020|Python Video Tutorials|40 Comments

40 Comments

  1. Neil Waghmare January 19, 2020 at 1:17 am - Reply

    I have one question.
    When will I need to decorate my functions

  2. how can i write a decorator that will accept an argument outside of just the function it decorates?

  3. F0rscher January 19, 2020 at 1:17 am - Reply

    perfectly laid out, other videos didn't make the logic clear, really good, thank you.

  4. Ed Tix January 19, 2020 at 1:17 am - Reply

    Hi! You've passed an argument to the decorator too: @wraps(orig_func)
    What is that mean? Is this just equivalent of passing additional argument to decorator function?
    For example: def my_logger(orig_func, my_argument)….. and then just using this argument inside decorator function?

  5. fishbert20000 January 19, 2020 at 1:17 am - Reply

    Thanks a lot for your tutorials from Germany! Being totally new to programming, I'm making my way through your python playlist with ease, as you explain them very precisely.
    One thing I haven't understood here is the necessity of the wrapper function.
    Why does the following code without the wrapper function not work?:
    ________

    import time

    def timer(orig_func):

    t1 = time.time()
    result = orig_func(*args, **kwargs)
    t2 = time.time() – t1
    print(f'{orig_func.__name__} ran in {t2} sec.')
    return result

    @timer
    def display_func(name, message):
    time.sleep(1)
    print(f'{name} says: {message}')

    display_func('Fishbert2000', 'Why not working?')

    ________

    It seems like somewhere on the way the arguments get lost and I'm taking a guess that it has to do with closures?

  6. gaby1491 January 19, 2020 at 1:17 am - Reply

    now i understand the power of decorators, thank you. liked and subbed

  7. Funny News January 19, 2020 at 1:17 am - Reply

    Hi Corey Schafer

    How to pass "request" in decorators?
    my Django code:
    def decorate_trigger(notice):

    def wrapp(*args):

    def runcode(request):

    uid = request.session['uid']
    code with "request" here can't run??
    return notice(*args)

    return wrapp

    @decorate_trigger

    def stream_handler(notice):

    print('———————get a dict from stream of pyrebase————–')

    my_stream = db.child('notifications').stream(stream_handler)

  8. Sankhadip Mazumder January 19, 2020 at 1:17 am - Reply

    I personally don't use functools as we can dynamically update the function name within decorator itself

  9. Luqman S January 19, 2020 at 1:17 am - Reply

    Very clear explanation, thank you very much!

  10. Raj Chiranjeevi January 19, 2020 at 1:17 am - Reply

    Hi Corey, Thanks for awesome video. Could you please explain on how does @wrap works. If possible, give some reference on the same. Thank You !

  11. Azzam Codes January 19, 2020 at 1:17 am - Reply

    My thanks to pateron/paypal supporters to this guy , he deserve it , supporting this is helping on having good content for all and supporters are part of this good work

  12. MonkDoc January 19, 2020 at 1:17 am - Reply

    Thank YOU!!!!!

  13. Keshini Patel January 19, 2020 at 1:17 am - Reply

    Thank you .

  14. Chris M January 19, 2020 at 1:17 am - Reply

    Great video, thank you!

  15. Anindhya rao January 19, 2020 at 1:17 am - Reply

    def f() :
    print('hi')
    print(f) the output is : <function f at 0x000001276EF88F28>
    print(id(f)) the output is 12367690957

    what is the difference ??

  16. EX0stasis January 19, 2020 at 1:17 am - Reply

    You taught this better in 30 minutes than my university professor taught this in 2 hours.

  17. alexander January 19, 2020 at 1:17 am - Reply

    decorator-seption?

  18. Ibrahim Nezar Al-Mahfooz January 19, 2020 at 1:17 am - Reply

    A decorator is something that can ONLY be well explained by Corey Schafer.

  19. Kshitiz Koirala January 19, 2020 at 1:17 am - Reply

    21:51 shouldn't time (wrapper function) be printed out first? but the original function is running first?
    Why is this happening? Doesn't this void the whole concept of decorators??

  20. Jia Ming January 19, 2020 at 1:17 am - Reply

    Sexy voice

  21. twitter bot912 January 19, 2020 at 1:17 am - Reply

    Thank you!

  22. blowfish fugu January 19, 2020 at 1:17 am - Reply

    looked into the package what they did there.. functools::wraps really rewrites _name_ and other attributes of the wrapper-func xD

  23. linda ph January 19, 2020 at 1:17 am - Reply

    Corey is the best! Thanks so much!

  24. Jis Mathew January 19, 2020 at 1:17 am - Reply

    I like your way of teaching, you build up a concept right from its base explaining from its roots and by the end of the tutorial you show the possibilies one can implement using the functionality.

    The possibilities you show inspires one to try it in their code, you have opened doors for pythonistas across the globe to raise their standards.

    Thanks Corey

  25. okba ramdani January 19, 2020 at 1:17 am - Reply

    You the best

  26. Ciao! January 19, 2020 at 1:17 am - Reply

    Can’t we not achieve the same by using class approach with abstract and interfaces?

  27. Almeida Cavalcante January 19, 2020 at 1:17 am - Reply

    11:13 this is where the real code kicks in…

  28. m January 19, 2020 at 1:17 am - Reply

    make my day

  29. vengat eswaran January 19, 2020 at 1:17 am - Reply

    Thanks Corey. Also I see this video related to Decorators seems interesting..
    https://www.youtube.com/watch?v=Zfr81m1chDI&t=2188s

  30. Tudor Gradinaru January 19, 2020 at 1:17 am - Reply

    what program are you using ??pls someone

  31. Jack Kensik January 19, 2020 at 1:17 am - Reply

    I would just define a variable as a lambda with no arguments, the only downside is that python only allows one line lambda's

  32. Linda Phan January 19, 2020 at 1:17 am - Reply

    Thank you Corey! Your videos are my all-time go-to!

  33. Randi Arisman January 19, 2020 at 1:17 am - Reply

    I don't know why, but I never watch full ads before on any youtube channel, but this is the first time I watch all 2 ads, 1 in 2 minutes because I could not support you by money or else. All of your good videos makes me still able to work in my current dying job with my dream that getting my new job in my bright future. Thank you, Corey!

  34. Bio Hazard January 19, 2020 at 1:17 am - Reply

    9:27 ultimate truth!

  35. burbon ­ January 19, 2020 at 1:17 am - Reply

    I feel like this @decorator could be overused. It blocks you from wrapping functions with other decorator.

  36. Elham Karami January 19, 2020 at 1:17 am - Reply

    Wow, perfect explanation! Thanks! 🙂

  37. Kourosh Jafari January 19, 2020 at 1:17 am - Reply

    If my_logger is applied to two different functions, the results are written into one log file generated from the first function, not two log files. Why is that?

  38. Tian Hao January 19, 2020 at 1:17 am - Reply

    learn a lot from ur video,make more plz

  39. edgar lip January 19, 2020 at 1:17 am - Reply

    Hi , as usual best video by far on the topic , i also wanted to mention ( may be it was mentioned before ) that

    @Decorator_Class // —-> is the same as display = Decorator_Class(display)
    def display():
    print('display function ran n')

    *this is regarding the oop part.

  40. yomajo January 19, 2020 at 1:17 am - Reply

    One thing might have been left out unsaid: Once decorator is in place, there's no direct, straightforward way to use function without decorator.

Leave A Comment

*