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

Learn Git with no prior experience
To be honest, learning Git is quite easy. I really don’t understand the fuss that’s going on about it being tough to learn. Let me take you through the basics first. Before starting with...

Why Python
Python can be used in any futuristics technology A= Analytics Data Science Artificial Intelligence(AI) Neural Network(NN) Natural Language Processing(NLP) Computer Vision(OpenCV) In Analytics...

PEP (Python Enhancement Proposals)
Python Enhancement Proposals(PEP) are suggestions for improvements to the language, made by experienced Python developers. PEP 8 is a style guide on the subject of writing readable code. It contains a...

Relational operators
Operation Syntax Function Ordering a < b lt(a, b) Ordering a <= b le(a, b) Equality a == b eq(a, b) Ineqality a != b ne(a, b) Ordering a >= b ge(a, b) Ordering a > b gt(a, b)

Using Lambda and Filter with Regular expressions.
1) Start the Python interpreter and import the re module: >>> import re 2)Define a tuple strings to filter with regular expressions: t=('python','pycon','perl','peril','step','stop','come','came','exercise','hello','storm','my.mail','year','yam') ...
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