What does args[0][‘valid’] mean here in decorator

@authenticated decorator allows the function to run is user1 has ‘valid’ set to True:

user1 = {
    'name': 'Sorna',
    'valid': False #changing this will either run or not run the message_friends function.

def authenticated(fn):
    def wrapper(*args , **kwargs):
        if args[0]['valid']:
            fn(*args, **kwargs)
            print(f'You are not authenticated to send messages. Please make deposit of $5 to your account')
    return wrapper

def message_friends(user):
    print('message has been sent')


I am having troubles understanding why args[0]['valid'] is used. When I used args[1]['valid] I got error, I know i’m missing some key concept. please help me out here


For def wrapper(*args , **kwargs) -> args represents the dictionary (user1), the if statement takes the dictionary (args[0]) and then takes the key ‘valid’ (args[0][‘valid’]), this way if the content of the key ‘valid’ is False you won’t send the message (else statement) and if the content is True you will send the message.

Try changing the content of the valid key as follow:

user1 = {'name': 'Sorna','valid': True}

Under these circumstances, the message will be sent.

Leave a Reply

Your email address will not be published. Required fields are marked *