Quick actions

cmd+k|ctrl+k

Navigation

Languages

Decorator:Authenticator

Snippet info

Language

Python

Visibility

public

Author

bensruthy

Created

2026-03-29T14:31:48.921183Z

Updated

2026-03-29T14:36:37.376777Z

# Create an @authenticated decorator that only 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):
    # code here
    def wrapper(*args, **kwargs):
        if args[0]["valid"]:
            print(args[0]["valid"])
            return fn(*args, **kwargs)
        else:
            return print("invalid user")

    return wrapper


@authenticated
def message_friends(user):
    print("message has been sent")


message_friends(user1)
INFO