I want to know where I am lagging. I am new to python decorators. How can we pass keyword arguments to decorator as parameter?
import time
import math
def deco(x,y,*args,**kwargs): #I want to pass kwarg like="andrew" here
def short(func):
def longer(r,*args,**kwargs):
print("before func exe")
begin = time.time()
func(r)
end = time.time()
print("after func exe")
print("I am ",kwargs['like'])
print("Total time taken in : ", func.__name__, end - begin)
return longer
return short
@deco(3,4,like="andrew")
def greet(r):
r = "I will be selelcted"
print(r)
# print("I am sorry for demanding")
print(greet('r'))
Here if we try to pass like="andrew" for **kwargs it throws error. how to pass it to deco then?
I also tried likes this.
def deco(x,y,like):
def short(func):
def deco(x,y,'like'):
def short(func):
What is the mistake I have done in above methods?
It throws error. KeyError: 'like'