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

All About Python You Need To Know Before Starting
Python for Beginners------Anyone can learn Python Prerequisites to learn Python To learn Python we don’t require the knowledge of any other programming languages. To learn all other programming...

Difference between Python 2.x and Python 3.x
Python 2.x and Python 3.x differs in following features: Division operator print function Unicode xrange Error Handling __future__ module

Python programming - Applications
If you’re thinking of learning Python? Or if you recently started learning it? You may be asking yourself: "What exactly can I use Python for?" There are so many applications for python, but there...

10 Best Python IDEs
Python is a very popular programming language. It's open source and used today as scripting language, web development, mobile and desktop in many areas thanks to the endless modules options it has While...

Mind, Body and Soul
When there is harmony there is synchronisation, when there is synchronisation tasks just flow one after the other in relaxed and orderly fashion, the root to all this synchronisation and the root to synchronisation...
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