Understanding Python Decorators, Schemes and Mind Maps of Computer science

The concept of decorators in Python, which are a powerful and versatile tool that allow you to modify the behavior of functions and methods at runtime. Decorators offer a clean and elegant way to wrap, enhance, or modify the functionality of your code without changing the original code itself. how decorators work, how to use them to improve your Python code, and provides examples of common scenarios where decorators can be used. It also explains the concept of first class objects in Python and how it is important for decorators.

Typology: Schemes and Mind Maps

2022/2023

Available from 03/11/2023

rahul-srivastava-3
rahul-srivastava-3 🇮🇳

1 document

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Demystifying Decorators
Photo credit to Maia
If you're a Python developer, you've likely come across
decorators in your code or heard about them in
discussions about Python's advanced features. In
essence, decorators are a powerful and versatile tool in
Python that allow you to modify the behavior of functions
and methods at runtime. They offer a clean and elegant
way to wrap, enhance, or modify the functionality of your
code without changing the original code itself. In this
blog post, we'll explore what decorators are, how they
work, and how you can use them to improve your Python
code. So, let's dive into the world of Python decorators!
pf3
pf4
pf5

Partial preview of the text

Download Understanding Python Decorators and more Schemes and Mind Maps Computer science in PDF only on Docsity!

Demystifying Decorators

Photo credit to Maia

If you're a Python developer, you've likely come across

decorators in your code or heard about them in

discussions about Python's advanced features. In

essence, decorators are a powerful and versatile tool in

Python that allow you to modify the behavior of functions

and methods at runtime. They offer a clean and elegant

way to wrap, enhance, or modify the functionality of your

code without changing the original code itself. In this

blog post, we'll explore what decorators are, how they

work, and how you can use them to improve your Python

code. So, let's dive into the world of Python decorators!

The fundamental idea behind ...

Most of us must have come across the word first class

objects in python. Fancier as it might sound it just means

you can pass them to other functions as arguments,

returned as values, and be stored in variables and data

structures. Decorators simply uses this property of python

to modify the behaviour of the original function by

wrapping it within another function. A simple yet elegant

syntax to represent decorators is the @ symbol followed

by the name of the decorator function just above the

function you want to decorate.

Think of it as a cherry on top of a cake used as a

decoration ;)

If you're wondering about the scenarios where decorators

can be used, the common scenarios include ...

Adding a logger or timer to a function without

changing the function code.

implement authentication or authorization checks

before function execution.

memoization which is a technique of caching the

function result to improve performance.

the result of the original function. The decorative task

here being the display of execution time of the function.

How to use decorators

But how do we use this decorator function in our code.

Well python being super intuitive and super easy to

understand provides a syntactic sugar abstracting away

all the complexities being handled under the hood. Lets

say i have a function named list_students() which as its

name suggests gives out a list of students based on some

logic. The below code shows how to find the execution

time for the execution of the function.

@timer def list_students(): students = ['Rohit', 'Virat', 'Pujara'] return students

Behind the curtains

For most of us the syntactic sugars work fine. But there

are few whose pet ants dont let them sleep without

knowing where this sugar came from. So lets deep dive

into the internals of how using a decorator function with

an "@" just above the original function works. In the above

example what python does behind the scene looks

something like this

list_students = timer(list_students)

Python functions being first class objects can be passed

to other functions and can be assigned to variables. You

must be realizing here how important the concept of first

class objects is for decorators. So as you can see here we

have passed our list_students() function as argument to

timer() func which does nothing but return its own

internal function whose task is to execute that

function and perform the decorative task. This is the

basic step for any decorator. The internal function is now

saved to the variable whose name is same as that of your

original function. Coincidence?

Well actually python very smartly overrides the original

function with the internal function returned from the

decorator. So now when you call list_students() you

actually are calling that internal function. So no

coincidence but sheer elegance.

Decorate function with params

In the above code we looked at a very simple case of

decorating a function which had no parameters. But if the

function has some params we dont see any place where

the params could be passed with the original function. In

function and can be accessed through *args and

**kwargs. So inside our wrapper() function when the

original function is called then those params can be

passed to it. Play a little with the above code by printing

the args and kwargs values to get a better understanding.