我试图在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')
看起来很直接,但我得到以下错误: 输出:
> 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
您正在返回wrapper的值。相反,只需返回函数。你知道吗
在返回包装函数时移除括号
相关问题 更多 >
编程相关推荐