UrbanPro
true

Learn Python Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Decorator in Python - Simplified

Logeswaran
15/06/2018 0 0
By definition, a decorator is a function that takes another function and
extends(/decorates) the behaviour of the latter function without explicitly modifying it.

Let see how we can apply decorators for Functions or Methods

Level I:


We might heard the term, 'sending a function as a parameter to another function'

Practical Example:

def firstFun():
print ("I am the firstFun")

def secondFun(inp): # we are receiving a function(inp=furstFun) as a parameter
inp()
print (" I am the secondFun")

secondFun(firstFun)

Output:
I am the firstFun
I am the secondFun

Observation from the output:
We sent one function(firstFun) to another function(secondFun) as a parameter, and
it helps to utilize that function(firstFun) according to the need.

Level 2:
Now we are going to see the same functionality but pythonic way(Decorator)
def secondFun(inp):
inp()
print (" I am the secondFun")

@secondFun
def firstFun():
print ("I am the firstFun")

Output:
I am the firstFun
I am the secondFun

*** That's it, Now you have done with what & how about Decorators in python***

Now, Again to the definition of the decorator,
By definition, a decorator is a function that takes another function and
extends the behaviour of the latter function without explicitly modifying it.

From our example program,
we are sending firstFun to secondFun and according to the need, we can add more behaviour along with
the original function(firstFun) without modifying it(firstFun)

For More Python Notes

0 Dislike
Follow 2

Please Enter a comment

Submit

Other Lessons for You

A program to calculate Correlation Coefficient
Task: Calculating the Correlation-coefficient using Python We know that the correlation coefficient is calculated using the formula nΣxy- ΣxΣy / (√(nΣx^2-(Σx)^2)...

Python- 5 reasons to learn python
1. Dynamically Typed You don’t have to declare a type when declaring a variable. It skips headaches of type casting JAVA:- int x = 1; x = (int) x/2;...

Different types of data in python3
Python3 features Numbers(int,float,complex), String, Lists, Tuples, Dictionary and Sets type of data. long interger type numbers from python2 is no longer required. Below are examples of all python3 data...

Topics covered in Python Basics course
Chapter 1: Introduction Introduction to Python Setting up path Chapter 2: Variable Integer String Operators (Arithmetic, logical, relational) Chapter 3: Data Structure Tuple List Dictionary ...

Build an interactive Dictionary using Python 3.x
I have downloaded a 5MB data base consisting of words and their meanings in a json file . I want to write a python progrsm which will ask the user to enter a word and then the program will search through...
B

Biswanath Banerjee

0 0
0
X

Looking for Python Training Classes?

The best tutors for Python Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Python Training with the Best Tutors

The best Tutors for Python Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more