Hi, this is Amanda with the school decorators. And this is our last video of the series decorators in Python to discuss a lot of things. In this video, we'll be discussing the exercise that they gave you the idea. Let's move on. Once again, repeating the exercise question. Make a decorator that decorates function with one argument, the decorative take in an argument that is the type of the data that is being provided.
And then it does a decorated admin function to check if the input is the correct data type or if it is wrong, and it should print in case of wrong data type. For example, you pass on any teacher and the function required in a teacher. So that is a correct data type. And if we pass on our string and our What an integer data type you should bring round at a time. Okay, so this is actually check type function that we need to create here we're going to check if data type that is being provided for example, in teacher so it should only accept in teacher or if it is a string or any other agent only accept string. So this is the decorator that we need to create characters.
First of all, we are creating a function type check. And we are passing the actual type of data that we do make sure it is being provided by the user. So inside that we are going to create another function that is going to take the old function that needs to be decorated. And within that, we are going to create another function that is going to be a new function and it flows Check actually, whether the argument provided is the correct data type or not. And how do we do it we already know that there is an existing function is instance that takes in two arguments. One is the data type that we need to check and one is we need to check if it is the correct data type or not.
For example, we pass on in teacher shear and like that is a number three or four or five or any number and we mentioned that it is it needs to be an integer so it is going to result in show as it is going to certain false. For example, if we require the data type EU provided as a string and here we pass on three, it is going to result in false I'd show you in wg school about how it works. So if you're looking to check in number five, and it should be an indigenous, which is going to result and show what it does, if the specified file object is of the specified type, and otherwise, it gives pause. So this object needs to be of this type. So I hope now this function is clear to you. This is already a built in function in Python, so we can already indirectly use that.
And in case if that is true, we're going to return the whole function with the same argument as again to print that type and, and this decorator function negated it done the new function and then the old function. Let's run this. Okay, it's already ran. Let's move here in another function time, And we are multiplying the number with three and we are decorating this time c function with the check type to ensure that the correct data type is provided and we have passed on in teacher as the object and a specified object that it needs to check. So let's run this. Okay check type is not defined.
Let's define that in. Okay, we have a training the functional stipend is good check. And here we are using Jenkins You need to pick this and now it should run properly that creating any error multiplying times three with three, so it should give nine okay perfect. And here we are passing a string value into time string and ideally it should print around type, because this is not a number and we pass this function, a decorated type check. So, it should check it and reserve should be auto number or there should be a wrong data type bedtime and there was nothing invented because this is not that not an integer Look checking this with strength value. So we are passing a word or maybe a sentence into the second letter and printing the first well sec actually is first index value.
Let's do this. Check in just type in make this check is done. Let's see, what which valuate Pass which a friend and the second actually letter because this is the zero index and this is the first index and this is a strength. Safe rented. E And that's perfect. And when I passed a list into it, it's not a strength, it's a list by the list.
So it should even added that on that time. But I hope initially the exercise looked a bit difficult but now it's very simple as you have already seen, because we already use an existing function. That is, is instance. And the other things were really simple. Just needed to create a decorator pass on check it with the instance function and it was done. Okay, this is the end of the course.
I hope you now have an excellent understanding of one of the advanced concepts, Python and that is decorators and you already know the ins know It's important semantics. It's workflow logic and we already saw a number of examples and he also did an exercise. So to be able to create decorators yourself, may you have any queries feel free to contact me and if you are looking to get connected, contact me on my email. That is the ag kuma@gmail.com Connect me on Twitter. It's Gus tech to see USD ch to connect me on Udemy and on medium Thank you