Python Decorators in 1 Minute!
Вставка
- Опубліковано 10 лют 2023
- ⭐ Join my Patreon: / b001io
💬 Discord: / discord
🐦 Follow me on Twitter: / b001io
🔗 More links: linktr.ee/b001io
Background Music:
Rain, Book And Cup Of Tea by | e s c p | escp-music.bandcamp.com
Music promoted by www.free-stock-music.com
Creative Commons / Attribution 4.0 International (CC BY 4.0)
creativecommons.org/licenses/... - Наука та технологія
I can't believe decorators were this simple. After watching a lot of full length videos on decorators, this is the one that finally clicked. Nice job mate 👍
you could've typed what is a python decorator into chatgpt and it would've given this exact example.
Same here!
actually there is more to them. Normal decorators have three levels instead of just two
@@clover6480yeah but the main idea is that a decorator receives the function under it and returns a new function. 3 levels thing you are talking is a function that returns decorator when it is called. Then python uses that returned decorator as usual.
Very good explanation, I wish I had it back when I googled "What is that @ in python before a function def" and found a 3 pages explanation on site 2 in the results.
I had the exact same experience (well I gave up after a while)
Chatgpt ftw
@@poiuytrewq1553 There once was a dark time, no COVID and no ChatGPT.
In all seriousness ChatGPT hasn't been here forever, and many beginners struggled to find information if they don't know what to search for. I personally struggled, and would have loved to have ChatGPT around.
thankfully we have chatgpt now
never showed the results for me because google always dropped special symbols in the search query which i find pretty annoying
If only there were more videos like this on YT. Straight to the point, and ONLY the point. Very well explained in such a concise way. Thanks!
Great short but very helpful video! I am new to python and was unaware of this ability!
I'm happy to see you are aware now
Same my friend. There is always more to learn and it’s nice to stumble across these videos every now and again.
WTF man, I've been struggling for a good while with the topic listening to various explanations from different people and you made it clear in a minute?! Thank you so much and keep up the good work!
"Python sees this @ symbol and understands that this function under it needs to be passed into a function called tictoc."
This is the kind of explanation i need.
Short and compact, gets the points through.
I finally found someone who explained it in practice. I watched a 2-hour lesson on functions, decorators and decorators didn't make any sense to me at the end of watching it. But your 1 minute video put it all together for me and now I understand, thank you very much
If someone can explain something difficult in simple words - he masters that knowledge. That’s about this guy for sure
this is the best explanation on decorators I have ever seen. All other videos repeat the same formula, almost like making a cake, but they always confused me by using overly complicated terms...
this channel is saviour to may of us new python coders. The thing which was difficult for me to understand in a video of 20 minutes he taught it in a simple way in less than 60 seconds.
You'll grow and you'll help us learn too. Thanks buddy
Thanks for doing my idea, now i understand what it does. Great video as always!
Same
This was nice! Well done
love the brevity of your channel!
What an awesome video! Simple explanation and in 1 minute!!! Congrats!
Best explanation of decorators in Python I've ever heard.
Short, concise, to the point and exactly what I needed.
BRO! I've watched like 30 minutes of videos about decorators and was 100% lost. You just made it perfectly clear in 1 minute. NOICE!!!
You did something simple and IMHO pretty intuitive, yet somehow so much tutorials lacks this. You've explained the case on a real live example. People would left and right explain all of that by saying that it allows to pass methods "b" and "c" into method "a" automatically, what is somewhat true, but is as easy to understand as teaching driving by explaining laws of physics that cars obey. So kudos for the great explanation.
I would love some one-on-one training. Whatever I need to do to be as sharp as you. These videos are great.
Like others in the comments section I've watched other, much longer, videos on the subject and was still left confused. In ONE minute you have provided the most simple explanation! Thank you.
You killed it, brother!
Can u make a 1 min short on decorators with parameters
I’ve seen this in other languages but no one explaining it ever used the key term “Decorators”. Thank you for making me aware of this!
Lots of nice tricks for this learner in one short video. Thanks.
We use a closure function that's gonna have access to the main function which is gonna send our decorated function as parameter and inside the closure it will be decorated. A decorator is easier to understand as a wrapper that receives our function as parameter to be used inside another function.
I'm following you for your simplicity and profound understanding. You just helped a brother.
wow it's a very great explanation
I was so confused about decorators earlier on.
This is probably the clearest explanation on decorators I've ever seen.
Your explanations are amazing!
Dont usually comment on videos but this was really helpful! Good job
Literally the best explanation on the internet!
God, I love this kind of tutorial, quickly and very understable...
to be a little more specific, decorators, afaik, simply take your function and replace it with whatever function is returned by your decorator, in this case the wrapper function
this makes use of closures, which means that functions keep whatever data they've been given access to when returned, even if the scope they were defined in is gone
this means you can also pass other stuff to decorators, and that can be very useful to create stuff like unit tests
This was helpful as I wasn't sure why the wrapper function was necessary .
Great video. I watched 4 other long videos about this without fully understanding. After your 1 minute video it's all clear to me. It's a lot like the time function in Bash.
I actually learned alot from this video.
If only I knew this a week ago, it'll make my assignment way easier
Straight to the point. Fantastic.
Very quick but very good lesson, thanks!
bro that is dope
Very nice and straight to the point :) definitely keep this up, there is a shortage of intermediate+ videos on Python
Look up the channel mCoding, he’s fantastic
the compsci class in my physics major calls this "introductory", now I'm scared of what's to come
This was a short but informative way to explain this concept. Very helpful, thanks
THAT. WAS. AWESOME!
short, precise, perfect
literally the most effective way to explain decorators
Finally a good python tutorial. Most short tutorials on UA-cam are just: "Want to solve this problem? Just use this library and it'll do it for you"
Thanks for saving me in coding interview,Nice video!
THANKS!!!! YOU ARE AMAZING!!!
woah ...1 min and you nailed it
Absolutely amazing content bro! A complete python tutorial for beginners would be priceless, pls try to make one 🙏
Neat! I used this recently for a SocketIO Python project but didn't really understand how it worked behind the scenes.
I'd like to pile on the praises here to boost your channel. Back when I was learning Python, wrappers were one of the things that I was having difficulties understanding. That, and *args and **kwargs. Both of which I understood clearly in your videos. Keep up the good work, and hoping success for your channel.
Wow! I never thought I would understand!
amazing explanation!
Dammit, this is great! Gonna use this at work tomorrow!
NICE AND SIMPLE! LOVE PYTHON!!!!
That’s pretty amazing!!
a crisp explanation with a good example of how a decorator function functions (pun intended)
One thing I am always confused about decorators is to use () or not while @func, also why define a wrapper and then return it, couldn't just execute those statementa directly in the decorator function
ikr
Words simply can't explain that.
I tried to type it out here, but I deleted all my writings after I wanted to talk about parameter handling and the use cases of it together with classes
To answer your second qs.
If wrapper() is called, then the function is execited and the decorator function returns a None Object which is not callable. This means that any function the decorator wraps will become non callable.
Hope this makes some sense.
To better answer your question, the reason why is because you need the decorator function (tictoc in this instance) to return a FUNCTION. The decorators job is not to execute the function, but to place more code around another function, to be executed later, thus by returning a function, you have something you can call later.
if you were to do away with the wrapper function, in the current example of the video, there would be nothing to return, which would lead to the explanation that Sufian Haq provided
Here's an easy way to understand this:
i) @example
ii) @example(foo="bar")
iii) @example()
in (i), because the function is not being called, '@' just takes `example` function to pass the decorated callable object to. Similar observation can be accepted normally too:
```
>>> def func(): ...
...
>>> func
>>> print(func)
```
in (ii) and (iii), because the function is called, '@' will wait until `example` returns something and take that value to pass the decorated callable object to.
To conclude, `@`: `` should be a callable that accepts the decorated callable object as its first positional argument, this callable's result will be replaced as the new definition of the decorated function.
```
import time
def dec(x):
t1 = time.time()
x()
print(time.time() - t1)
@dec
def func():
time.sleep(2)
# results are shown at runtime without having to call `func`.
```
```
class Foo:
def __init__(self, text: str):
print(text)
def dec(func):
return Foo
@dec
def func():
...
func("hello world")
```
```
class Foo:
def print_text(self, text: str):
print(text)
def dec(*, text):
def wrapper(func):
print(text)
return Foo
return wrapper
@dec(text="hello world")
def func():
...
func().print_text("bye world")
```
These are working examples. A decorator is simply a function and is expected to return something callable to pass the function object being decorated. Python gave it a unique syntax because they can and because the syntax makes sense. Decorators can also accept arguments because it's just a function as I said.
Important part of decorators is ability to use initial function without any changes side by side with our new decorated version. So you need to import wraps from functools and to use it in your logic.
What the actual heck bro. I have been learning python since 2015 or so and never understood decorators no matter how much it was taught. You did in 1 minute what 8 years couldn't. Thank you!
That explanation was faster than the pop-up window timeout asking for my email when I use the SEO'd top google results.
just brilliant
Good interview I learned a lot
I was bashing on Python quite a lot recently, but damn this is actually a really really good functionality!
This was indeed very helpful!
I wasn't expecting to learn new things about python today, but I'll take it
I never realised decorators were just fancy way to call normal functions, I have seen so many of them but was never able to remember the syntax until now
That is a legendary explanation.
sooo good
This example finally drove it home for me
I had no idea this existed. I'm doing this asap
Incredible helpful I didn't know that thanks
Hey! This really helped! But What is the purpose of the wrapper? Can it be done without it?
Same doubt, did you get it lateral?
Thanks for this. Most helpful and well explained.
The only intelligent explanation of decorators! Thank you very much!
small tip, that \ at the end of the first string in print is not needed. inside parentheses, strings on multiple lines are concatenated with no joiner between them.
Your videos are so awesome!
Already knew this. Liked for other patterns
Finally somebody explained it right! THANK YOU!
man i love your videos
Great explanation. Thanks. Nit pick: in this fantastic example, time.time can be replaced with perf counter for precision.
so good
Just amazing, thank you ever so much!
WOAH thank you
Yes, now i understand. Thank you.
jesus christ i understood it in no time, been doing the angela python course 4 fcking videos and i cant , get lost in all the functions she keeps naming function....... thank smy guy you are god sent
The beast of explanation.
This example can hardly be improved.
Great Video, thanks for the info
Suupa video. Helped me understand decorators in general ! Though, I have to ask. Does the same logic applies to JS future ? (Currently Typescript) ?
This channel is awesome :D
Dude I love you.
Beautiful
Basically:
@deco
def func():
....
is the same as
func = deco(func)
Ohoh..
it sounds slightly similar to lambda, maybe, should think about that
😮 thanks u guy somuch. Now i got it
Wow, how simple! Decorators are just wrappers. The wrapping around a box is sometimes decorative. :)
Amazing explanation!
What theme do you use for your VS Code? It looks kinda cool and clean
What font and theme do you use? That looks fancy
fantastic explaination! Thank you for making this I now completely understand. After staying to myself, 3 minutes ago, "hey what is that @ symbol? It seems important."
omg! I was always wondering what they did
Good stuff
Excellent