Created
October 25, 2019 13:00
-
-
Save sly01/b58b3ddfe170d486c3fa5343a065bceb to your computer and use it in GitHub Desktop.
Decorators with parameters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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