Quick actions

cmd+k|ctrl+k

Navigation

Languages

decorator

Snippet info

Language

Python

Visibility

public

Author

brseeni

Created

2020-01-01T18:14:59Z

Updated

2020-01-03T01:57:26Z

def decorator(func):
    def wrap(*args, **kwargs):
        print('*********')
        func(*args, **kwargs)
        print('@@@@@@@@@@')
    return wrap

@decorator
def hai(*args, emo=':('):
    print(*args,"-----", emo)

hai('i can pass parameter on decorator','121','usly','2222','serio','so long',':)')



# Create an @authenticated decorator that only allows the function to run is user1 has 'valid' set to True:
user1 = {
    'name': 'Sorna',
    'valid': True #changing this will either run or not run the message_friends function.
}

def authenticated(fn):
  # code here
  def wrap(*args, **kwargs):
    if args[0]['valid']:
      fn(*args, **kwargs)
    else:
      print('user is not authorized')
  return wrap

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

print(user1['name'])
message_friends(user1)
INFO