Skip to content

Instantly share code, notes, and snippets.

@sly01
Created October 25, 2019 13:00
Show Gist options
  • Save sly01/b58b3ddfe170d486c3fa5343a065bceb to your computer and use it in GitHub Desktop.
Save sly01/b58b3ddfe170d486c3fa5343a065bceb to your computer and use it in GitHub Desktop.
Decorators with parameters
import functools
user = {"username": "ahmet", "access_level": "guest" }
def make_secure(access_level):
def decarator(func):
@functools.wraps(func)
def secure_function(*args, **kwargs):
if user["access_level"] == access_level:
return func(*args, **kwargs)
else:
return f"No {access_level} permissions for {user['username']}."
return secure_function
return decorator
@make_secure("admin")
def get_admin_password():
return "admin: 1234"
@make_secure("guest")
def get_dashboard_password():
return "user: user_password"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment