def check_permission(user): def decorator(func): def wrapper(*args, **kwargs): if user["access_level"] == "admin": return func(*args, **kwargs) raise Exception("No way!!!") return wrapper return decorator user = {"username": "jose", "access_level": "admin"} @check_permission(user) def get_admin_pass(): return "1234" print(get_admin_pass())
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter