If I have a function that looks like:
@app.route('/categories/id/<int:id>/edit')
@login_required
def edit_category(id):
#some code...
And the login_required decorator looks like this
def login_required(f):
@wraps(f)
def wrapper(*args, **kwargs):
print id #given to edit_category by app.route decorator
return f(*args, **kwargs)
return wrapper
How do I access the id variable that is given to edit_category as an argument by the app.route decorator, from the login_required decorator?
The positional arguments to the wrapper function come in the args
(first argument - *args
) , and the keyword arguments for the method come in kwargs
(second argument - **kwargs
).
args
is a tuple , where the first element refers to the first positional argument, second element is the second positional argument.
kwargs
is a dictionary , where the key is the keyword (for the argument) and the value is the value passed in for that keyword.
Example -
>>> def decor(func):
... def wrapper(*args, **kwargs):
... print('args - ',args)
... print('kwargs - ',kwargs)
... return func(*args, **kwargs)
... return wrapper
...
>>> @decor
... def a(*a, **b):
... print("In a")
... print(a)
... print(b)
...
>>> a(1,2,x=10,y=20)
args - (1, 2)
kwargs - {'y': 20, 'x': 10}
In a
(1, 2)
{'y': 20, 'x': 10}
You can test whether app.route
is sending id
as positional argument or keyword argument by printing both args
and kwargs
and take the correct value. I think it may be coming in as positional argument, if so, it would be the first element of args
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.