Python Decorators Made Easy

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

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

magneat
Автор

You forgot to "decorate" your line 7 with a return, before func(a, b). Good stuff anyway, thanks.

JoaoBR
Автор

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.

JasonGabler
Автор

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

DanielTrivino-qnbr
Автор

These videos are gold for beginners like me.. short, concise and most importantly precise and to the point!
Please keep these coming.!!

hoola_amigos
Автор

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!!!

kannanraja
Автор

Dude you saved my next seven days to understand and make decorators work.

MayurPatil
Автор

You know the concept of decorators genuinely and you have explained it so beautifully. Thank you Francis!

vijaynrao
Автор

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

Piece
Автор

It is the better explanation what I'v seen! Thank!

iden_naturalist
Автор

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.

bsmaheshkumar
Автор

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

blenderremastered
Автор

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.

kochcj
Автор

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?

davidpham
Автор

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

classicrockonly
Автор

It's so clear to understand, the easiest and clearest explanation

Jun-qjmz
Автор

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

atreushouse
Автор

Does decorator always require a nested function?

THOTHO-ielz
Автор

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.

sc
Автор

If you return a/b in insider function, it will not through error for non zero values. by very nice explanation. really appreciated.

Piece