Python Tutorial: Decorators With Arguments

preview_player
Показать описание
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:

#Python
Рекомендации по теме
Комментарии
Автор

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 :)

mudithhimwan
Автор

I believe this comment will help many people out.
At the end of this video, people could be confused because they think, "A function has to be passed into the decorator, but in Corey's example, the string 'log:' was already given as an argument! Then does this mean *display_info = prefix_decorator('log:')* ?"
I have an answer. Nope. it is *display_info = prefix_decorator('log:')()*
Do you see the *extra* brackets at the end?


A good way interpreting this would be using a substitution.
Looking at the return value of 'prefix_decorator function', we know that
*prefix_decorator('log:')* equals *decorator_function*

Therefore

equals


Resulting in
*def print_func():*
*print('x')*

being the same as
*def print_func():*
*print('x')*

which is eventually
*def print_func():*
*print('x')*
*print_func =

xcxfgpn
Автор

Hey Corey, I came to your video from my Udemy lesson. I ALWAYS DO. You explain everything so simply. I wish you had a Full Python tutorial.

youngsinatra
Автор

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

italonesi
Автор

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

disooqi
Автор

just looking at the code you are showing in the beginning solved my problem, thanks! 👍🏻
and the rest of the video taught me something i wasn’t looking for. Excelent.

apffer
Автор

I thought this would take an hour or so to understand, thanks for breaking it down into 6 minutes aha. I understood it all!

elliot
Автор

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

sumitsinghal
Автор

you have natural talent for teaching. Thx!

vitalss
Автор

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.

andreasmiles
Автор

I see these decorators all the time in wrapper libraries for discord API and telegram API, now I can understand what they're doing :)

edsonmatheus
Автор

I was wondering, with out decorators, just to see logic:


')(display_info)

piotrmazgaj
Автор

Can you explain how it is actually being called . How is the function being passed to the decorator_function(), shouldn't we get an error that it is expecting a function but none is being passed.

chetanbajaj
Автор

Another good tuts Corey. Looking forward for some flask tutorials in the future. You are always amazing. Thanks and goodluck!

junar
Автор

Just for some more clarification.
Here display_info is going to referent
<function at 0x109313670>

sriramrimmalapudi
Автор

Thank you so much! I'm learning so much out of your informative videos
God bless you!
thanks thanks

muntadher
Автор

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.

iambhanu
Автор

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.

haoyangrocks
Автор

Bit off topic, but there doesn't seem to be any functools.wrap method for class decorators? Why then do some people prefer using that?

scullyy
Автор

Great series, thank you!
Screen resolution is often too high. Rule of the thumb: All text should be readable on a 13 inch screen.

visit shbcf.ru