UrbanPro
true

Learn Python Training from the Best Tutors

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

Search in

Decorator Advanced ( Passing Parameter )

Logeswaran
15/06/2018 0 0

Hope you read our previous note about the basic concept of python-decorators, if not, please read first


Let see what are the possible ways to utilise the decorators in real time.

Well, here we are going to learn the important 3 parameters passing into the decorator

1) Passing parameter to the decorator
2) passing the function as a parameter to the decorator
3) passing parameter to the function which we are passing to the decorator

DECORATOR FUNCTION - Usually kept hidden from the programmer as part of framework,
and, allow the programmer import this file to use

def moneyConversion(ConvertInto, InvestmentAmount):
moneyValue = {'USD': 61, 'SGD': 55, 'AUD': 51, 'DIRHAM': 18}
def Wrapper(InterstCalculator): #Wrapper is not a keyword, we can have any name for this method
if ConvertInto:
amount = InterstCalculator(InvestmentAmount)
newMoney = {}
for person in amount:
newMoney[person] = amount[person] / moneyValue[ConvertInto]
print newMoney
return Wrapper

InvestmentAmount = {'christ':1000,'Guna':5000, 'Rita': 1500, 'Habib': 3500, 'David': 5000}

interestRate = 6 # 6% (but we are not using this variable anywhere)

# It is our usual function that find the interest(amount*6/100) amount for the investors money
@moneyConversion('USD',InvestmentAmount)
def findInterest(Amount):
interestAmount = {}
for investor in Amount:
interestAmount[investor] = Amount[investor]*6/100
return interestAmount

What can we understand from the above example?

1) @moneyConversion('USD', InvestmentAmount) - This is the way we can pass a parameter to the decorator

2) The function(findInterest) will be passed to the Wrapper function which exists in the decorator
     i.e., InterstCalculator = findInterest

3) Finally, we are extending the functionality of find interest into converting the INR to other Currencies

4) Decorator plays an important role in Framework development like Flask, Django, Robot                 

    Framework, etc.,

0 Dislike
Follow 2

Please Enter a comment

Submit

Other Lessons for You

Datetime Module
#this examples demonstrate use of datetime module import datetimeob=datetime.datetime.now()print("-"*25)print(ob)print(ob.year)print(ob.month)print(ob.day)print(ob.hour)print(ob.minute)print(ob.second)str1=str(ob.hour)+':'+str(ob.minute)+':'+str(ob.second)print...

5 Tips On How To Become A Web Developer
1. Have A Goal: Always first decide what you want to create. 2. Learn To Code : You should be passinate about coding. As you know practice makes a Man Perfect. 3. Google for Solutions : Someone once...

Learning a new technology
Each and every day new technologies are getting introduced. Its quite clear that to be part of the fast moving professional world you should be upto date with the cutting edge technologies in the market....
S

Saurav Singh

1 0
0

Use IPython shell for better understanding of python
Hi All, Even though there are many editors like IDLE, PyDev, PyCharm are available for python, it is very advantageous to use ipython as an interactive shell for Pyhon. How to install it? pip install...

Debugging Python code
If you are getting an error while executing your python code, the better way of running your python code is by using -i switch. ex: python -i scriptname.py once you run the above command, your execution...
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