A Guide on What Are Python Decorators and How To Use Them?
This post is on Python Decorators. We’ll discuss the application of Decorators with examples and ways to create and play with it.
In Python, Decorators have special meaning they can be called on other methods or functions using @ symbol. With decorators, you can extend or change the functionality of python functions.
Prerequisites to understand python decorators concept
Python Decorators can be considered as an effective solution to numerous challenges. Before you proceed we advised you to learn python programming tutorial.
Which we have for you in this link of introduction to Python programming.
Let us start with a simple example by using function has an object. In Python functions, classes are associated as objects.
def add(a,b): c=a+b return c some_variable = add print(some_variable) print(help(some_variable)) #PYTHON OUTPUT Help on function add in module __main__: add(a, b) ~ ~ ~ ~ ~ ~ Help on function add in module __main__: add(a, b)
Here add()
the function takes two arguments a,b
.We have assigned add method to some_variable
a variable.
As you can see in the above example we are calling help()
on some_variable
and you may be curious about what functionality help()
function provides.
In Python help()
the function helps programmers to view details of the function, module, or any kind of object. The programmers can get to know the documentation and details of an object.
Now some_variable
has all the properties of add()
the function. So insisted on calling add(15,10)
we can call some_variable(15,10)
.
def add(a,b): c=a+b return c some_variable = add print(add(15,10)) print(some_variable(15,10)) #PYTHON OUTPUT 25 25
Till now we understood that function can we assigned as objects to variables and then can be used to call function. In Python, we can go more further bypassing function objects as arguments to another function.
def add(a,b): c=a+b return c def calling_func(func,a,b): print(func(a,b)) a=10 b=10 calling_func(add,a,b) #PYTHON OUTPUT 20
We are passing add()
function as python object to calling_func()
function by just giving add
function name. We don’t need to mention the arguments for add()
function.
As you can see we are passing arguments only to get results from add()
function.
Till now we understood that function can be used as an object and passed to other functions as arguments. Now will learn to create our first decorator.
We’ll be calling simple_decorator( func )
is decorator function it takes a function as arguments. But we don’t need to provide function name while calling @simple_decorator
on function.
def simple_decorator(func): print("Function name passed in decorator is : {}".format(func.__name__)) def wrap(): print("Entered wrap() of simple decorator") func()#calling function from decorator return wrap @simple_decorator def cal(): print("function cal called...") cal() #PYTHON OUTPUT Function name passed in decorator is : cal Entered wrap() of simple decorator function cal called...
When we call cal()
function first simple_decorator()
executes than in simple_decorator()
function there exists an inner function called warp()
.We’ll be calling cal()
function inside wrap()
.
Some more examples for Python Decorator
def check_for_error(func): def wrap(a,b): print("The values for division are : {} / {}".format(a,b)) if b == 0: return print("The value cannot be divide by zero") return func(a,b) return wrap @check_for_error def divide_func(a,b): c = a/b print(c) divide_func(10,25) #PYTHON OUTPUT The values for division are : 10 / 25 0.4
Here we are passing arguments to divide_func()
and as you see that in warp()
function inside decorator we can retrieve the arguments which we passed to `divide_func()
.
def has_permission(my_func): def inner(role): if role.upper() == 'ADMIN': return my_func(role) else: return print("This user is not authorized.") return inner @has_permission def display_profile(role_name): print("Welcome Admin...") role_name = "Admin" display_profile(role_name) #PYTHON OUTPUT Welcome Admin...
role_name = "Admin" display_profile(role_name) #PYTHON OUTPUT This user is not authorized.
Ladder Decorators
Decorators can be stacked one above the other.The top decorator is the first decorator to execute and the execution is passed to the next decorator so on. These are helpful in various cases suppose we need to check the user role and only allow a specified role to view the message.
def has_permission(my_func): def inner(role): print("Entered has_permission inner()") if role.upper() == 'ADMIN': return my_func(role) else: return print("This user is not authorized.") return inner def change_role(func): def inner(role): print("Entered change_role inner()") role = 'Admin' return func(role) return inner def another_decorator(func): def inner(role): print("\n*******************\nEntered another_decorator inner()") print("This decorator does nothing its just for example you...\n*******************\n") return func(role) return inner @change_role @another_decorator @has_permission def display_profile(role_name): print("Welcome Admin...") role_name = "Staff" display_profile(role_name) #PYTHON OUTPUT Entered change_role inner() ******************* Entered another_decorator inner() This decorator does nothing its just for example you... ******************* Entered has_permission inner() Welcome Admin...
The above example will give you a lot in-depth in working with decorators. We have included @change_role
this will change the role from Staff to Admin. So that we can access display_profile()
through has_permission
the decorator.
First @change_role
executes followed by top to down till last @has_permission
decorator.




