Python for Developers — 100daysoflearning (PART 9)
Day 71–74
In continuation to the course for Python For Developers lets move on with next set of learning topics.
Python programming language is very powerful language. It also have some ideas and functionalities implemented as functional programming because the functions are objects. In this article you will learn about decorators and how these can be extended to functions.
Decorators: as per the official documentation :- A function returning another function, usually applied as a function transformation using the @wrapper
syntax. Common examples for decorators are classmethod()
and staticmethod()
.
There is a concept of higher order functions which means a function that receives a function as an argument or returns a function is refereed as higher order function. Lets create a simple function in order to understand this concept which looks a bit complicated.
Above you can see myfunc is a function and it has func as an argument and ‘*args’ represents the arguments to the function ‘func’. So when you call the function ‘myfunc’, you need to pass the function and arguments of that particular function that you have passed. Before you find it confusing lets run it and see how it works:
What I have done here is I have called myfunc and passed the function sum and the since there are two arguments that is defined for ‘sum’ function so I have passed two arguments while calling myfunc, therefore you can see the output.
This was just a simple example higher order function but the real use case come when you try to define a decorator that takes function as a argument.
Lets modify the code and use the decorator syntax, *args for the arguments of the function and **kwargs for the keyword arguments.
I have put the updated program and the output in the same screen. As you can see that above the function sum I have used ‘@myfunc’ which means that when I call the function sum it will in-turn call the function ‘myfunc’ as pass ‘sum’ and its parameters to ‘myfunc’ , myfunc returns ‘wrapped_func’ and ‘wrapped_func’ return the value . So without the actual implementation for the function sum you are able to print the implementation of it with the power of decorators. You can define multiple decorators above the function sum and it will call them one after the other with the return of previous one.
Lets look at some common decorators now : classmethod, staticmethod and property.
Here you can see that a simple User class is defined first which has a class variable url , next it has a classmethod called ‘query’ decorator which means a class level method and it takes class and query as the parameter and return the result, next up is a class level staticmethod that takes no argument and return the predefined result and the last one in this section is property so it is property of the class and not a method exactly as when you call User.full_name it will not result in the name but actually prints the output . So property decorator is actually a class and not a function . Lets see all of the implementation of above program and see whats the output:
If you read the theory for the functions and then see the above output, things will be more clear. Its not a complex topic but actually very useful in making complex stuff easy.
In this article we have discussed a unique topic which generally is not present in all the programming languages and python makes is very easy to use decorators and make complex stuff easy as I mentioned before as well. So we have seen classmethod, staticmethod and property decorators. There are more decorators as well which you can look out and learn from the documentation but the most commonly used are these ones. Also the more interesting one for the complex use-cases is the property one so lookout for the property in the docs in order to get more clarity on the property decorator. try out different examples and send them out in responses so that we all can learn and grow together .
Happy Learning & Happy Coding
Saiyam Pathak
https://www.linkedin.com/in/saiyam-pathak-97685a64/
https://twitter.com/SaiyamPathak