Python Decorators

I found this great post on Python Decorators. Decorators were introduced into the python language since 2.4. The post shows the benefits of using a python decorator to improve the efficiency of a function’s runtime using memoization.

Python decorators are also extensively used in Django for authentication. For example, you can use them to decorate a view function so that only logged in users can view certain parts of your site.

from django.contrib.auth.decorators import login_required

@login_required
def my_view(request):
    # ...

The example code above shows that you only need to add the @login_required decorator above your view function to determine whether or not the current user needs to be authenticated before the view can be displayed.

One thought on “Python Decorators