#44 Python Tutorial for Beginners | Decorators

#44 Python Tutorial for Beginners | Decorators

Introduction to Python Decorators

In this video, the presenter introduces decorators in Python and explains how they can be used to add extra features to existing functions without changing their code.

Defining a Division Function

  • A division function is defined that takes two parameters and returns their quotient.
  • The function is called with two arguments, and the output is printed.

Modifying the Division Function

  • A new requirement is introduced where the numerator should always be greater than or equal to the denominator when dividing.
  • The logic for swapping the values if necessary is added to the division function.
  • The scenario where you don't have access to modify an existing function's code is presented.

Introducing Decorators

  • Decorators are introduced as a way of adding extra features to existing functions without modifying their code.
  • A decorator named "smart" is defined that accepts a function as its parameter and returns another function that modifies its behavior.

Implementing a Decorator

  • An inner function named "inner" is defined inside the smart decorator that performs the desired modification on its input parameters before calling the original function.
  • The inner function swaps its input parameters if necessary so that the numerator is always greater than or equal to the denominator when dividing.

Understanding Decorators in Python

In this section, the speaker explains how to use decorators in Python to change the behavior of an existing function at compile time.

Creating a New Function

  • A new function is created that takes a function as a parameter.
  • A function is defined inside this new function which replaces the code of the original function.

Changing Function Behavior

  • Before calling the original function, its definition is changed using the new function.
  • This changes the way the original function works and allows for changing its behavior at compile time.

Benefits of Using Decorators

  • Decorators allow for changing the behavior of an existing function without modifying its source code.
  • They are useful when you want to add functionality to an existing codebase without breaking it.
  • Python's functional programming capabilities make it possible to create functions inside other functions, allowing for decorators to be used effectively.
Video description

Check out our courses: AI Powered DevOps with AWS - Live Course :- https://go.telusko.com/AIDevOps-AWS Coupon: TELUSKO10 (10% Discount) Complete Java Developer Course Batch-4: https://go.telusko.com/Complete4 Coupon: TELUSKO10 (10% Discount) Master Java Spring Development : https://go.telusko.com/masterjava Coupon: TELUSKO20 (20% Discount) Udemy Courses: Spring: https://go.telusko.com/udemyteluskospring Java:- https://go.telusko.com/udemyteluskojava Java Spring:- https://go.telusko.com/Udemyjavaspring Java For Programmers:- https://go.telusko.com/javaProgrammers Python : https://go.telusko.com/udemyteluskopython Git : https://go.telusko.com/udemyteluskogit Docker : https://go.telusko.com/udemyteluskodocker For More Queries WhatsApp or Call on : +919008963671 website : https://courses.telusko.com/ Instagram : https://www.instagram.com/navinreddyofficial/ Linkedin : https://in.linkedin.com/in/navinreddy20 TELUSKO Android App : https://bit.ly/TeluskoApp Discord : https://discord.gg/D8hWe9BqfF 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 :- https://github.com/navinreddy20/Python-