Skip to content

Commit 025863a

Browse files
author
SanjayPradeeo
committedSep 23, 2018
Decorating with Parameters
Decorating with Parameters with exaple
1 parent 6adbf8b commit 025863a

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
 

‎Decorators/WhatIsDecorators.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,30 @@ def ordinary():
110110
def ordinary():
111111
print("I am ordinary")
112112
ordinary = make_pretty(ordinary)
113+
114+
# Decorating Functions with Parameters
115+
# The above decorator was simple and it only worked with functions that did not have any parameters. What if we had functions that took in parameters like below?
116+
117+
def smart_divide(func):
118+
def inner(a,b):
119+
print("I am going to divide",a,"and",b)
120+
if b == 0:
121+
print("Whoops! cannot divide")
122+
return
123+
124+
return func(a,b)
125+
return inner
126+
127+
@smart_divide
128+
def divide(a,b):
129+
return a/b
130+
131+
divide(5,2) # Output: I am going to divide 5 and 2
132+
133+
print (divide(5,2))
134+
135+
# Here's the Output of above statement ..
136+
137+
# I am going to divide 5 and 2
138+
# 2.5
139+

0 commit comments

Comments
 (0)