HOW TO USE DECORATORS IN PYTHON 2022 (THE RIGHT WAY)
Вставка
- Опубліковано 12 жов 2022
- In this video I will be showing you how you can use decorators in Python which can help make your code cleaner, and much easier to read.
▶ Become job-ready with Python:
www.indently.io
▶ Follow me on Instagram:
/ indentlyreels
As one of you kindly pointed out, "*kwargs" should actually be "**kwargs" to function properly inside the wrapper. I happened to miss that in the video, sorry about that!
I was the whole video waiting for the code to fail on you but you never sent a kwarg in the end to find the bug. Easily done. :) Particularly nice lesson this one. I never add doc stings so don't use wrap but if you write a library it absolutely would be the way to go. Or you could just nest the doc string but that's admittedly less elegant.
Is that a "bug" though? It's doing exactly what you'd expect form the code; the wrapper is being returned, not the parent function. Meh, potential undesirable / mildly inconvenient.
This is the clearest example I've seen on this topic. Thank you very much! Here you have a happy person that finally get over the frustrating feeling for not being able to understand this. Keep up the hard work!
Congrats on the video, it's simple and helps a lot. This functionality in python is realy useful to repetitive validations needed in functions
These videos are simple and to the point. Helping me to build my project.
You should pass kwargs with **, i e func(*args, **kwargs)
I scrolled down to comment that.
Cool guide, learned something new. Thanks :)
I would also like to see the annotations for the return type of the functions. A nice to have considering that you're annotating the arguments in the video.
Good explanation!
types.MethodType or types.FunctionType (?), if you must.
Love your content. Succinct & informative.
Have been using this similar snippet of code for months now I have no idea what wrapper does, now I know why (and fortunately it isn't harmful!) Thank you
Nice! I didn't know about wraps
VERY USEFUL & ELEGANT video on a popular (and often MIS-used!) concept - NICELY DONE !
- Mark in North Aurora IL USA
Amazing, Thank you for all the content!
Thank you for following along mate!
This is cool. Thanks
great, thanks
Hi nice video. Is the @wraps(func) required.
You're indeed the best one in Python, I've ever watch on UA-cam. I like your video where it comes alongside with full explanation, concise and more specific. I wonder how long have you been professional in Python?
I don't think I would call myself a professional as much as a Python enthusiast who enjoys reading the documentation ahah. I'm really happy you enjoy the videos, thanks mate! There will be plenty of more to come!
Not to hate on this video or anything, but if you want to see other good Python tutorials check out mCoding
@@jacksonbourne I'm a big fan of mcoding
Nice video
How would I handle an async generator in a decorator?
Nice video! What font are you using for pycharm?
JetBrains Mono
What if do_something returns a value? I think wrapper should do result = func(*args, *kwargs) and return result at the end
Doing that will make it raise a NameError exception.
Edit: Nevermind, I think I understand. You need to add a new return statement in the same place.
I Confirm you are World's Professional Python Programmer
Quick question: What if the function do_something(param: str) has a return value? How does the decorator get_time work?
Won't work Will always return None, cause is missing return func(*args, **kwargs) inside wraper function
Save the return value when you call func() inside the wrapper() function and then return it at the end of wrapper().
def wrapper(func):
....
ret = func(*args, **kwargs)
...
return ret
Should use DOUBLE asterisk to correctly parse keyword arguments into keyword arguments, if you use one asterisk, it will parse into the positional argument.
And to make sure wrapped function's return returns its result properly, should save the return of parameter (inner) function and return it from inner function again.
You're absolutely right, I honestly didn't see that I only wrote 1 until now
cool
Nice contents. What is missing is how to pass parameters to the decorators
You're right, I missed that! I'll make another video in the near future covering that.
Yes, I’d like to see this too, the only way I know right now is by using yet another layer of wrapper, so you basically have two nested functions. You have a function that takes your special args that creates a decorator that takes the function that creates the wrapper that takes args and kwargs…..
@@preritdas6998 I will look into it, but this sounds like the only approach I know of at the time being.
You can put any expression as a decorator,
That means you can put a function call that returns the decorator:
@ === @ === @function() -> @returned_decorator === used as decorator,
But when you do simply:
@ === @ === used as decorator
Not a bad video, but I don't see why the 2022 modifier had to be added in the video. It implies there's something new to this, but wraps() has been around for quite some time.
It's common to include the year in a programming video to show that the information is still relevant in that current year. Thanks for the comment!
@@Indently it's not common, this is the first and only time I've seen this
@@Indentlyfair enough. I've seen this on quite a few videos myself
@@guinea_horn you must never watch any videos about coding, game development, or api interactions ever.
1st view Bro
❤️
I get "NoneType object is not callable" error, why is that?
did you remember to return the wrapped function from the decorator?
There’s something almost meta about using a wrapper to fix wrappers.
I'm not so impressed with that either ahah, there's probably a good reason for it (maybe there isn't), I'll have to look deeper into the documentation for that.
There's something I don't get it.
1. what is "*args" and "**kwargs" ? what does "*" mean?
2. Why can't we just put that timer function code in the function "get_time", but define a function called "wrapper", and then "return wrapper"?
"args" and "kwargs" is just a name. programatically, "*" (in a function definition) means ARGS for the interpreter, additionally ** means KWARGS
it has just become standardized to use args and kwargs as their names but
def myFunc(*a, **b)
would work too, just its a bit non standardized.
And what's a wrong way then. ?
instructions unclear, year is 2023 and my computer exploded :P
Man, you serious? This was already added to python...thanks for revision btw.
for the longest time i've been wondering what decorators are for. Been watching a lot of videos and this is the one that made it click for me. right right I kinda understand why dash uses @callback or @app.callback now.
functools.wraps does noe prevent a bug, because there was never a bug on the first place. Wrong choice of words