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

web scrapping using scrapy Python
Overview of Scrapy Scrapy is a Python framework for large scale web scraping. It gives you all the tools you need to efficiently extract data from websites, process them as you want, and store them in...
S

Sharma academy

0 0
0

Python Demo
https://www.youtube.com/watch?v=8AEhpZ4lbOI

Python is a popular programming language. It was created by Guidovan Rossum, and released in 1991.
Python is a popular programming language. It was created by Guidovan Rossum, and released in 1991. It is used for: web development (server-side), software development, mathematics, system scripting. What...

A python program using the datetime module
Task : To write a program in Python to find out, in any given year, Friday the 13th dates i.e 13th day of a month which was a Friday. ...

A Python Program to calculate Variance and Standard Deviation
Finding the variance and the standard deviation. Variance and Standard Deviation are related measures of dispersion that tell us more about a list of numbers,like are they...
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