A decorator is a function that wraps another function. Say you have a function f (x), and you have a h (x) decorator, the decorator function takes your f (x) function as an argument, and therefore essentially what you will have is a new function h ( f (x)), It does for cleaner code, for example, in your login_required, you do not need to enter the same code to check if the user is registered, and you can wrap the function in the login_required function to call such a function only if the user is logged in. Check out this snippet below.
def login_required(restricted_func): """Decorator function for restricting access to restricted pages. Redirects a user to login page if user is not authenticated. Args: a function for returning a restricted page Returns: a function """ def permitted_helper(*args, **kwargs): """tests for authentication and then call restricted_func if authenticated""" if is_authenticated(): return restricted_func(*args, **kwargs) else: bottle.redirect("/login") return permitted_helper
cobie source share