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

Benefits of Design Patterns in Application Development
Application developments is a tedious job. Programmers write code and test it again and again to make sure their software works fine. So designing complex software is a hard nut to crack. Skilled programmers...


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 ...

Simulation of a die-roll:Usage of random module
Task:Simulation of a simple die-rolling game,where we keep rolling a six-sided die until we have rolled a total of 20. To simulate a six-sided die roll, we need a way to generate a random integer between...

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;...
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