python-3.xpython-decoratorsargs

Python Decorated Function Args


I am facing a weird issue where sometimes my decorated function is receiving arguments and sometime not.

The code is as such:

main.py

@Authenticator.authenticate_admin
def action_snapshot(username, token, payload):    
    .
    .
    .

authenticator.py

class Authenticator:

@staticmethod
def authenticate_admin(func):
    
    @wraps(func)
    def wrapper(*args, **kwargs):

        print(f"ARGS: {args}")

        # ! Execute function
        return func(*args, **kwargs)
        
    return wrapper

So if i try to call the function in my main like that:

action_snapshot(username, token, payload)

the code works fine and i receive the args.

However if i call it like that:

action_snapshot(
    username=username, 
    token=token
    payload=payload
)

the args is empty.

Any idea why ?


Solution

  • You're still receiving them, but because you're passing them as keyword arguments, they're in kwargs instead of args