0

I'm having trouble accessing the default keyword argument value within a decorator. If I pass in a value to the decorated function's keyword parameter, I can access the keyword argument value in the decorator just fine. Otherwise, I cannot access the keyword argument value.

Here is a working example of a decorator and a decorated function:

def print_args_kwargs(func):
    def wrapper(*args, **kwargs):
        print(args, kwargs)
        return func(*args, **kwargs)
    return wrapper

@print_args_kwargs
def say_hi(text='hi'):
    return text

No dice:

say_hi()

>>> (), {}

This works:

say_hi(text="hello")

>>> (), {"text": "hello"}
irahorecka
  • 1,447
  • 8
  • 25
  • It's possible that what you really want may be achievable with `functools.wraps`. – LeopardShark Nov 27 '21 at 23:10
  • The default value isn't *passed* to the function; it's used *by* the function if no argument is passed. While possible to access a function's default argument values, it's not typically something you *need* to do. – chepner Nov 27 '21 at 23:35

0 Answers0