Python Decorators Made Easy
HTML-код
- Опубликовано: 30 апр 2018
- In this video we'll be learning about a cool feature in Python called decorators, we'll be learning how to use them and what they're for.
Go to howcode.org for more!
Link to DigitalOcean: howco.de/d_ocean
Link to howCode Facebook: howco.de/fb
Link to howCode Twitter: howco.de/twitter
Link to /r/howCode: howco.de/reddit
Don't forget to subscribe for more!
Correct code that works properly:
def check(func):
def inside(a, b):
if b == 0:
print("Can't divide by Zero")
else: return func(a, b)
return inside
@check
def div(a, b):
return a / b
You forgot to "decorate" your line 7 with a return, before func(a, b). Good stuff anyway, thanks.
Obrigado, não estava achando o motivo que ele imprimia None kkk
Nice one.. that was giving me errors not anymore ..
Great explanation! I believe it is missing 'return' before func(a,b) in the decorator:
def check(func):
def inside(a,b):
if b == 0:
print("Can't divide by 0")
return
return func(a,b)
return inside
Yes otherwise it will show 'None' value
Why is this better than a plain, straightforward call to the original function inside a new function without all the abstraction of decorating? This video is great at explaining the mechanics, but even one sentence as to why they are superior would be great.
This is what I'm searching for too. I understand how it works, but I don't understand why it's any better than writing a proper function that handles it's own checks in the first place.
These videos are gold for beginners like me.. short, concise and most importantly precise and to the point!
Please keep these coming.!!
You know the concept of decorators genuinely and you have explained it so beautifully. Thank you Francis!
What an amazing explanation, I have watched a lot of other YT videos to understand Decorators. But This one completely helped me to understand about the decorators that too within 4 Mins.
After trying to understand Decorators in many site, I finally turned to YT, and there it goes!! within 3 minutes understood why and what Decorators are! Good Explanation indeed!!!
It's so clear to understand, the easiest and clearest explanation
Does decorator always require a nested function?
Dude you saved my next seven days to understand and make decorators work.
wow this is the most concise short explanation from someone finding other explanations too hard to understand on decorator, in life people don't learn thing because some people are bad in explaining. big thank to you
Finally someone could reach me to teach me decorators! You earned a subscriber sir!
Awesome and thanks for the clarity u feed me on decorators
How does the check function get the a and b parameters though???
In what scenario would you do this instead of actually going inside and adding logic to the original function? Wouldn't that be easier, faster and less confusing than creating a decorator?
If you have to add the check to multiple functions, or you only want to temporarily modify the function, this is useful
I'm still trying to figure this out myself. (And since you ask this a year ago, you probably are good to go by now lol) But I know they can be useful when building a webapp, using something such as Flask. Inside your main Python file, you will have functions that will execute when some visit a page, and as you build it out, using decorators defining different routes can help you add multiple endpoints/routes to the same function.
@@samiam.402 @David Pham
One such scenario is when you wish to wrap behaviour around a function, such as a logger.
Another such case is when you want to take third party code (code you cannot change, like a library) and add functionality to it. You can simply wrap their functions with your decorators to get custom behaviour.
Finally, to combat the "actually going inside and adding logic to the original function", you can think of this in terms of functional programming perhaps. But you can use decorators to compose new functions, where they may share the same core functionality but you may not want to write the new functions in source code (perhaps there are many permutations), and instead build them dynamically.
What theme are you using?
I didn't even know it is possible to create nested functions in Python ^_^
That's good to know
is this something like callback in JS?
Thanks. Best explanation ever!!!
This is only useful when you don't have access to the original function, correct? Or, can you now use either version of the function, and if so, how would you call the original form?
That is an awesome explanation. Thank you!
Thanks this was super helpful. It also helped my learning to test and see that print(check(div)(10,0)) is valid syntax.
Awesome, clear and concise
won't func(a,b) get execute twice? Once for inside() definition and another time upon executing inside() via return?
That was simple and easy , I understand it now thank you
It is the better explanation what I'v seen! Thank!
Hello i accidentally stumbled on this video while looking looking for a wk to understand decorators since english is not my 1st language and you've explained it so well thank you
Thank you ! I was confused about the @ syntax.
Straight to the point. Thanks!
Apparently with Python 3.6.7 if I follow your code line for line the output is always "None", but once the decorator is removed it properly returns the divided value.
EDIT: forgot to add - at line 7 if you add return then your code is correct
can you please provide normal code that works? Instead of this NONE explanation in video :-/
@@magneat Hope this helps...I don't know how RUclips will format this, so format accordingly :)
#!/usr/bin/env python3
def check(func):
def inside(a, b):
if b == 0:
print("Can't divide by 0")
return
return func(a, b)
return inside
@check
def div(a, b):
return a / b
print(div(10, 0))
The major take away is that it is supposed to be "return func(a, b)", not "func(a, b)
classicrockonly yeah, thank you. Though I figured it out myself already. And didn’t you forget to put ELSE there?
@@magneat Nahh it's not necessary. If it's true it will return and ignore the rest of the inner function. So the else is implicit
classicrockonly btw, just today saw tutorial which said that putting single RETURN is “returning None implicitly”, which is considered a bad practice. And that Indian developers are doin this often in their code. “explicit is better than implicit” is named a better practice, like more correct way.
Best explanation I've found so far. Thanks a lot 👍👏
Wow, the explanation is easy to understand.
is this the same as function closure in javascript?
So, i have a question:
The inner function is useful because in that you can write the parameters of the outer function's parameter, wich is a function
That's not a question, that's a statement.
All I can say is-Thank you. I finally got it.❤️❤️❤️
Beautiful video: short, informative, and no BS filler.
the concept of decorators is very cool!
Awesome video. Very easy to understand.
The inside function should be returning the result of func(a,b), not just calling it. Without returning the result of that call, division by non-zero is just going to print out None.
If you return a/b in insider function , it will not through error for non zero values. by very nice explanation. really appreciated.
Thanks dude your video made me understand this concept with in 3 min :)
after watching videos of20,20 mins, I got my concept completely clear in your 3mins video! Thank you! keep going on
Thank you for explaining this
Great explanation in common english, thanks!
Great simple video. Thanks for that!!!
You explained it greatly and in 3 minutes. Great for a very quick and clear introduction! Compliments!
thank you. I know basics now.
nice neat and clean. thanks for the vid.
How come these examples always do something like div = check(div) as in the video. Would it work if he just wrote def check(div) in the decorator itself rather than def check(func), or does it have to be an empty parameter to start with?
in 'def check(func)" func is just a local name, it could be anythong, if you call this div then in line 7 you would need to change code from func to div
Proper good job mate.
very easy to understand . Decorator is just putting a function A before a function B in execution so that the whole execution is more desirable without editing function B. In other words, if you do not want to edit a function but you need another result from that function, just decorate it. Decorator is just a wrapper.
Thank you for this.
Very Good explanation. Thanks!
easily explained using this example... understood the basic and learned something new today.... can I have some more complex examples on decorators? or may be a small project that explains the usability of decorators in more advanced ways... thanks bro.
def check(func):
def inside(a,b):
if b == 0:
print("Can't divide by Zero")
else:
return a/b
return inside
@check
def div(a,b):
return a/b
print(div(10,2))
great tutorial!
Thank you!
beautiful python
Very clear, thanks!
Examples in this cases are essentials, and your example was perfect. Thank you!
If you must have access to the original function's definition in order to use the @ operator to declare a decorator function to replace the original function with, and doing so immediately replaces the original function with it's extended, decorated version, then why not just simply extend the function definition right there at the definition which you necessarily have access to? I don't understand how this is useful. Please halp
when implementing your code, when i try to divide 10 by 5, it does not give me an output
In line number 7, why we are calling func(a,b) that too in inside(a,b) function ?
Line 7 should be "return func(a,b)", otherwise "inside" always returns "None".
straight on point
good explanation thanks
Cool explanation for beginners like me👍🏻
Thank very simple explanation
Good explaination.thanks
Superb Video!
thanks for the video! but why it returns None, as it gets printed out? thank you!
"None" is the default value returned by python when nothing else gets returned.
Best explanation of decorators and you managed to do it under 4 minutes! Thank you.
good work.
func is executed twice?
nice explanation.. liked it
so basically decorators are functions that get called when their underlying function gets called, with a default first argument being the decorated function.
wtf
Exactly....
@@bsmaheshkumar5328 and the title is decorators made easy
Thank you
Thanks a lot broo
crystal clear!
good stuff, thanks c:
good explanation
how come you don't have 10 million subscribers yet?
good example!
Could not understand the need for the insider function. Why can not it be just the checker function ?
Simple is the best!
Can I use this to run Hello World or any function whatsoever?
Good stuff
thanks!
great video, gem
beautifully yet strong explanation of decorators...very useful...please post more videos on the key feature of Django/Python
thanks king
Nice onedark theme.
Awesome
just one suggestion, if you try to explain with doing debugging alongside your videos will be superb.
thanks
best i've watched
Write a function that takes the argument and transfers the factor of this number.
Order the function decorator, which also gets the int argument and the result of the result, making the decoration - raises the argument and returns the result
can i get a help? cant understand why my professor in university of computer science started teaching me from this shit...
Neat!
Thanks I was struggling lol
@howcode I didn't see "Something else" added in your earlier example. I only saw you type in an empty string. Furthermore, this would have been easier to follow if you did this within an IDE instead of going back and forth between an editor and a command line interpreter.
The concept is pretty clear (imo); I don't see much added value in persnickety requests; I mean, other visitors aren't complaninig at all, that makes me think that the guy accomplished his goal (sharing knowledge) and helped me with mine (understand the basics of how decorators work)... and probably, others' too.