Reputation: 93
I am trying to execute the below code in pycharm.
def auth_login(func):
def wrapper(*args, **kwargs):
print("Authenticating......")
func(*args, **kwargs)
return wrapper()
@auth_login
def login(username, password):
print("Successfully logged in:",username)
login('setu', 'setu')
Looks pretty straight forward but I getting the below error: Output :
> Traceback (most recent call last):
> Authenticating......
> File "C:/Users/611834094/PycharmProjects/PractiseProject/decorators/example3.py",
> line 10, in <module>
> @auth_login
> File "C:/Users/611834094/PycharmProjects/PractiseProject/decorators/example3.py",
> line 7, in auth_login
> return wrapper()
> File "C:/Users/611834094/PycharmProjects/PractiseProject/decorators/example3.py",
> line 5, in wrapper
> func(*args, **kwargs)
> TypeError: login() missing 2 required positional arguments: 'username' and 'password'
>
> Process finished with exit code 1
Upvotes: 1
Views: 823
Reputation: 79
Remove the brackets while returning wrapper function
def auth_login(func):
def wrapper(*args, **kwargs):
print("Authenticating......")
func(*args, **kwargs)
# Remove the brackets from wrapper
return wrapper
Upvotes: 1
Reputation: 3623
You are returning the value of wrapper. Instead just return the function.
def auth_login(func):
def wrapper(*args, **kwargs):
print("Authenticating......")
func(*args, **kwargs)
return wrapper # Here was the issue.
@auth_login
def login(username, password):
print("Successfully logged in:",username)
login('setu', 'setu')
Upvotes: 3