Function not working

users={“ajay”:“manusid”}

def login(fun):
def wrapper(username,password,*args,**kwargs):
if username in users and users[username]==password:
add(*args,**kwargs)
else:
print(“not authenticated”)
return wrapper

def add(a,b):
print(a+b)

add=login(add)

add(“ajay”,“manusid”,1,2)

hey @ajaysiddartha ,
The error that you are facing is due to the naming of variable.

In this line , you have reintialized add as wrapper function ,so when python reaches inside the actual wrapper function ,after checking the username and password , it finds add function to calculate the sum.
But , at this time , your add function is a reference to wrapper function . So it raises error.

So just change this to ,
add_ = login(add)
add_(“ajay”,“manusid”,1,2)

It will work fine.
I hope you understand this.

i didnt understand…

let me clear it again.

On calling this function, now add refers to wrapper function. Agree ???

So now , in the next command , add(“ajay”,“manusid”,1,2)
This will call your wrapper function with the respective parameters , username and password condition will be checked , but if now you notice , you next command is add(*args,**kwargs) , but you have this add now referencing to , wrapper , so now it won’t be able to directly sum up them and hence gives error.

I hope you understand now.

users={“ajay”:“manusid”}

def login(fun):
def wrapper(username,password,*args,**kwargs):
if username in users and users[username]==password:
add(*args,**kwargs)
else:
print(“not authenticated”)
return wrapper

@login
def add(a,b):
print(a+b)

add(“ajay”,“manusid”,1,2)

this should work no then

its giving wrong answer

No , it won’t be giving the correct answer , because it is again getting stucked.
just debug it by ,
Just before checking , username and password.
just add print(username, password, args, kwargs)
and keep everything else the same,
from output you will notice a kind of recursion. This due to this recursion that you are getting wrong answer.

I hope this helps.

yes i wrote add in the place of fun
thank u!!

Thats really nice that you got it.

I guess now your doubt is now resolved , i would request you to kindly mark this doubt as resolved and also too provide your valuable feedback.
Thank You and Happy Learning :slightly_smiling_face:.