#44 Python Tutorial for Beginners | Decorators
Вставка
- Опубліковано 28 лип 2024
- Check out our courses:
Spring and Microservices Weekend Live Batch : bit.ly/spring-live-weekend
Coupon: TELUSKO10 (10% Discount)
Master Java Spring Development : bit.ly/java-spring-cloud
For More Queries WhatsApp or Call on : +919008963671
website : courses.telusko.com/
Instagram : / navinreddyofficial
Linkedin : / navinreddy20
TELUSKO Android App : bit.ly/TeluskoApp
Discord : / discord
In this lecture we will learn:
- Properties of functions
- What are decorators in Python?
- How to add extra features to a function?
- Properties of Decorators
- How to define decorators in Python?
#1
- Functions are built to perform certain tasks.
Properties of functions:-
- We can also write a code for the function inside another function.
- We can assign a function to another function in python as a function is also an object.
- A function is an instance of the Object type.
- We can store the function in a variable.
- We can also pass the function as a parameter to another function.
- A function from a function can also be returned.
#2
- Decorators are used to add extra features to the existing functions.
- Decorators can change the behaviour of an existing function at the compile itself.
#3
Properties of Decorators:-
-The outer function is called the decorator, which takes the original function as an argument and returns a modified version of it.
- Decorator contains an outer function that also takes a function as an argument.
- Inside the outer function, there is another function that takes a number of parameters as per the logic as an argument.
- Inner function contains the code for the logic that must be contained in the previously defined normal function.
- Inner function returns the values as per the required code that can be equal to the number of arguments passes in an inner function.
- Outer function return an inner function.
- In the main code, we have to call the outer function of a decorator and pass the normal function as an argument.
Github :- github.com/navinreddy20/Python- - Наука та технологія
I just realized that you being physically present in the video makes such a big difference to how much attention I pay. There are tons of videos out there but your videos work for me because there is someone teaching and I can see them typing the code. The expressions and gestures also help with the learning, maybe subconsciously we understand better if knowledge transfer is backed by facial expressions. Thanks again for choosing to share your knowledge, for free!
The same I felt
The best is when he makes mistakes and you can see his reaction lol makes it more real
Me also for any video web cam is must
totally agree ..
Very very true...
You missed the most important part of the whole thing.
Using the @ syntax before the function we want to wrap is what makes decorators useful.
I had to watch another video that explains it to make me see that the way he explained it was also very good. I was just thrown off by the lack of the @ syntax.
I was waiting for the @ thing, or a part 2 of this too.
anyway this was a good video. Subscribed.
Same here 😒 went to another video. But this one is really good too!
Need the link for the other video, did not get this concept
what is this @ syntax all about, i dont know this can get any link
please
For those asking about the missing @, this guy actually explains what are decorators not how they are written. The syntax is unimportant if you don't know what is happening behind the scenes. The issue I observe with new programmers is that people are using stuff that they have no idea how is it working. I appreciate a great conceptual explanation.
Yep. That would describe me. But Telusko threw in some really good information here. So basically the decorator is redefining where the 'div' object is pointing to in memory right?
But I don't know how to use @syntax and why they use @wrap. It will be helpful if he do separate video on this
You are the hero of students who are eager to learn coding but can't afford to go for tution ,even me.hands off to my guru
Ok, have to admit that this was one of the videos that was a little harder to grasp. I do get the main goal of decorators, but I am searching for more examples on how to use decorators. Your video was a great first start. Thank you!
Try code with harry for this one than jump to telusko you will get the Idea. No offence i am not comparing these two channels they both have their own taste our main intention is to grasp the concept
@@akashsuna5160 don't promote that guy. India was humiliated 2 years ago during github's hacktoberfest because this moron misguided clueless people into participating in it. It was horrible thanks to him.
Sir you are simply awesome. To be honest whenever I stuck for any technical topic, I immediately search on UA-cam to check if there is any similar video posted by you. Your way of teaching is too good. Many thanks.
I am a postgraduate in electronics field. I thought programming would be difficult, but u made it as simple as that. You are a great teacher. I have been working as a lecturer since 8 yrs . I have never seen a person like u, who teaches with so much of energy and enthusiasms. I have to learn many things from u -way of teaching.etc......
your teaching is just awesome sir! I have seen blockchain videos and now am learning python just by seeing your python videos. its just wow! great work sir. Thank You.
yes, agreed with many viewers that Navin sir haven't used '@' in the whole explanation. But we all understood the concept of Decorators through his explanation, which most of the UA-camrs failed to explain.
Thank you so much for this video! I've been trying for an hour to wrap my head around decorators (pun intended :P) and this is the first video that's made sense to me.
Sir i saw the whole ad so that you get the reward😊😀
🤣
just click the add and close it. It works on a cost per click. It won't do anything if you just watch the add.
@@063dharmeshrajan5 your mindset is what I call 'chutiya' mindset. Clearly, you were never taught to give back. Tutors like Navin Reddy are teaching for free and can't you even watch a 30s ad for him? Nobody is asking how to get rid of ads here. @mohit and @MD are talking about supporting the video creator by watching ads. Get lost with your tip that no-one asked for.
@@SaFFire123x Sorry man , I'll predicted above 1 comment wrong I am deleted my above comment but my prediction is not to hurt someone just help for those whose study continuosly and got so much irritated from ads and for your sake I am watching all (studying)videos only on UA-cam version not Vanced version I know that it affects the earning but I know how and when do we use vanced version and please don't abused any one else again this show which type of background you come from okay I am ☮️ peacefully Sorry but do not use abusive language.
@@063dharmeshrajan5 Hey! Take it easy. I was only discouraging 'greedy mindset'. It's not just 5 minutes but 5000 hours of experience, pre and post processing that UA-camrs put into their videos. So the feeling of being morally obliged to give back should come from within. That's what I wanted to imply. No intention of hurting anyone.
I felt like you were publicising the use of vanced on a completely irrelevant and unnecessary thread. Glad that you deleted it. Peace ✌️ and happy learning :)
Really impressed! Actually I've been through 4-5 videos before, but no one cleared my doubts this easily, that also in just 7 minutes. ❣️
Really loved it.
I was confused in decorators, but this is the only video from which I really got my confusion cleared! Thanks man, Keep it up
The best explanation I've seen so far. I'm a Python beginner (I work with Javascript).
Isn' this almost exactly like javascript's closures?
Really helpful video for me and after a long time I have found a best teacher for guidance in programming .Thank u so much sir
Beautifully explained !! I was looking everywhere for a relevant real scenario to test the use of decorators..
This is the best and simple explanation of Decorators I ever seen, good job :)
what a explanation, great sir, none of other can explain like you.
thank you for tutorials.
Well explained, thank you
I'll advise you do videos on concepts such as first-class functions and closures; these would help to understand the concept of decorators better. Well done.
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.
smart alien !.. you taught me this in 7.32 mn where i was trying hard to learn this from other sources for the past few hours
Now I understand WHY I would use a decorator ... simply illustrated and simply understood. We all learn differently and this was concise.
Hey Navin, Thank you for sharing your amazing knowledge with you, This is the first video in 44 videos which I found a bit harder to grasp.
Exactly my thought 🤲
Hi. If the inner(a,b) function is the one that is responsible to add/modify/influence the code of div(a,b) function, then what is the need to write the inner(a,b) function within smart_div(func) function ?
Bro I want to know about generators and Itreators so please have these two in your next video 🙏🙏
Using the @syntax...
def smart_div(div):
def inner(x,y):
if x < y:
x,y = y,x
return div(x,y)
return inner
@smart_div
def div(a,b):
print(a/b)
div(2,4)
Sir thanks for the lecture literally u are doing a great job...!!!!
Sir I think we should also add an "else" condition so that if we type the larger number first it will also return the answer.
def div(a,b):
print(a/b)
def smart_div(func):
def inner(c,d):
if c
I did n't see anyone like your exaplantion your amazing i got clear idea about this
Best decorator tutorial I have found from youtube. Thank you!
That is the best explanation. Thanks
Sir why did we use ‘return inner’ in the end, I didn’t understand. Because ‘return func(a,b)’ is already doing the work of calling div(a,b) right? Then what is the use of return inner?
Really nice explanation. Well produced video. Thanks!
Before calling the function, what if I swap numbers (let's say prompted from user) checking inside if loop? Correct me if I am wrong.
Sir How can we call a function which is defined below in the function that is defined above it?
2:04, Our mom's reaction, when we don't do our housework in this corona time.
😂
Thank you Sir, now I have understood it properly.
Sir, Thank U for making decorators so easy. I have watched many videos but did not understood anything. Thanku and a great respect from Kerala 😊😊
Sir please make another video explaining this . Please sir .
Thankyou sir its really helpful .
How can we make the inner function global to use else where when setting up other decorators?
Can this be achieved?
i like this guy, he is so audible and straight on point
sir i have question.
what if we use extend of div class to create another class so that, we can use div class properties is this possible?
please tell me
2:04 - When a junior dev asks for help from a senior dev
Wao.. excellent explanation sir.... finally my confusion is clear!!
Sir when we defineing the function we must be call the function that's why the function it's works.but the inner function we don't calling how it's works?
This is the best example I found to understand decorator...😀
Your videos are always helpful, sir can u make some more examples for decorators? so we can get more clear idea of decorators.
how does inner get called and when? what i have understood is we are passing the div function as parameter to the decorator function, which is returning inner_function. Now, the inner_function object or address will be overridden in the ‘div’ because we are capturing the returned function in it. After this, whenever we call div, the execution goes to inner_function in the decorator.
this helped me understand better. thanks very much
Hello sir,
I would say your skills always astonish me .Basically I am from ECE background I am facing difficulties to understand these topics So I always used to feel low .but after watching your videos in no time I get motivated and inspired by the way you teach us.you are really doing great work.it means a lot for us.keep doing sir .thank you
Hello,
I want to pursue ECE because i have interest in electronics field.
Can you tell me that which coding language is most important for ECE students and if an ECE student wants a job in software company , then how can they join because they are not tought coding upto that much level which is required for software development.
Same here I am also from ECE background and this is the first language I am learning ... Thank you sir for teaching in such a way that the non it student also easily catch the concepts
hello sir,
The changes which we are making in the existing function are permanent or temporary ???
Sir - Can you advise what is the purpose of return used within smart div and inner?
Nice explanation sir :) , by the way, I think we can add '@' also right for using decorators instead of assigning the old function to the new one🤔
Thank you for the explaination :)
I have two functions which is used for serial com and other is ssh which is used to connect to target ...How to develop wrapper combining these two
I have been looking for decorators and explanation. It is possible that I might not be looking at the right place or with right keywords but not a single video explained it so u effortlessly as you did. Kudos.
Hello Sir thank you very much for explaining the topic. I have one question regarding how the value is passed . When the object div1 was created and then in div1(2,4) value was passed , the object div1 was an instance of the function smart_div () which was taking a function as argument only, hence how the object of smart_div which is div1 pass the value 2,4 and got accepted. It will be helpful if you clear my doubt
When we had Simple prgram initial with less number of linnes of prgam then why we should go with max lines in 2nd phase which decorators mode
hi navin..i got the concept what you have given but i am totally confused while practicing
Really you have great teaching skill sir.... Thanks for explaining decorators🙏
is new div permanently changed ?? what if I use put (4,2) now
Why do we have to write the logic inside the function of smart_div()?
We could have done that right after smart_div function isn't it?
Can anyone, please explain to me about this?
The way you teach sir is really very very smart😎
Can we do the same thing without using the inner function there ,so that we just make use of smart_ div function directly
U are a exellent mentor really awesome explanation and very very interesting the way u explained
In the above program, if we changed the values like div(4,2), it will not be returned output:(
ie. div function is not getting called.
we are passing func as input parameter of the fucnction how could that be working can you expalin in deatil
suppose the div() function was in a different file after importing it to a new file and changing using decorator......so will the div() in the original file change??
This could be the possible decorator systex
def smart_div(func):
def inner(a, b):
if(a
from math import sqrt
def modify(func):
def inn(a):
return a*a
return inn
sq=modify(sqrt)
print(sq(25))
print(sqrt(25))
My assignment sir 😁😁😁
Your presence makes the video so much better✨️
@Telusko : I did not understand the use case of this decorator here , can't we just wrap div() in side another function which will just swap the values and pass to div() ?
def div(x, y):
print(x/y)
def smart_div(x,y ):
if x
Maybe new arguments of div method will also swap. but not sure
Hi Navin,I have decide to learn the UI Path,please tell me how is about UI path Scope in India
This guy is a phenomenal teacher.
could you please create a detailed video on decorators for clarrification?
I really appreciate this. You simplified this topic well
Amazing explanation..sir...thank you thank you so much
very helpful, thanks for the video
Can you guys help me understand something; If the below code also works (ignoring the ZeroDivision Error) why is there a need to add the additional inner function, as is in the tutorial?
def div(a,b):
print(a/b)
def smartDiv(a,b):
if a
Can we modify the in-built functions using decorators?
you are amazing sir, thanks a lot
omg !!! No one can explain decorators better than this
Can anyone explain how inner function is getting arguments a and b, when smart function calls inner, it is not passing any arguments.
Thank you sir I understood this concept.
I was working on decorator topic and also saw 2-3 videos but was not satisfied by the logic. But your teaching made me understand the concept very easily. Thank you :)
How it's actually work can anyone say step by step how it's work ..?
At 6:25 how does the inner function get the parameters of div? How are a and b given into this construct??
so confusing
Sir, I have one doubt.
Can't I do like the code mentioned below?
def div(a,b):
print (a/b)
return a/b
def inner(a,b, func):
if a
Great teaching. But you missed @ syntax. and also can you explain it little deep about why we need to use inner functions etc. I am sure we can't explain decorator concept in just 7mins.
Sir could u please make one separate video on decorator elaborately
Thank you very much. You are a genius.
sir i didnt understand y should we write a another inner function in the new one.
cant we pass the inputs of div directly to smart div
no u cannot pass thats why we are using little bit tricky method
You can read more about decorator design pattern. That will clear as why we can't pass the values directly.
First learn what the following mean:
1.Python Closures
2.Python functions are first class objects
This is not a tricky method nor does it have anything to do with decorator design patterns.
Div can also be called as the method you told, but if we want any function to be decorated by passing to any decorator then, we need to define a inner function which represents the function object of the decorated function.
Hope you understand it well.
creating another function inner is necessary? can we not apply the condition inside the body of smart_div only?
this definitely helped - thanks. Can't say I still understand it to the bone, will have to re-watch, experiment & fail to 'get' it
sir i really enjoyed learning python playlist, but sir, i'm totally form mechanical background and in some cases i can't understand the what is happening in the code and why it has been written and i have the dout in the decorators i.e.
def div(a,b):
print(a/b)
def smart_div(func):
def inner(a,b):
if a
You could also do your operation by using a HOF that just receives a function as one of its arguments. No need to return another function. An example:
def division(a,b): return a/b
def subtraction(a, b): return a - b
def do_operation(operation, a, b):
return operation(a, b) if a > b else operation(b, a)
print(do_operation(division, 4, 12)) # 3.0
print(do_operation(division, 12, 4)) # 3.0
print(do_operation(subtraction, 4, 12)) # 8
print(do_operation(subtraction, 12, 4)) # 8
Decorators are one of things added to the list of "Never Use Unless you Want to Ruin Your Program". Tread carefully.
Thanks sir you have a good skills
Thank you Telusko, it helped