We are a small, intelligent App development studio. We love "Building Amazing Apps", solving problems and cultivating strong relationships with our clients.

If you used Django for any length of time, you would have come across the
login_required decorator. You write @login_required before a view, and it
magically becomes accessible only to authenticated users.

Decorators were introduced in python 2.4. PEP 318
is the PEP describing it. At
the simplest decorators are nothing but callables returning other callables, and
the decorator syntax @decorator is nothing but foo = bar(foo), where both
bar and foo are callables.

At this point, we created a new LogArgumentsDecorator object, so
LogArgumentsDecorator.__init__(self, sum_of_squares) gets called. Hence
print 'started logging: %s' % orig_func.__name__ line is executed.

When we do sum_of_squares(...)

LogArgumentsDecorator.__call__ gets called with the passed values, where we
print the arguments and call the original function.

Here is one more example of a class based decorator. Here we rewrite(sort of,
anyway) Django's login_required decorator as a class based decorator.

Subscribe to The Uswaretech Blog where we will be writing the next part of this tutorial explaining more complex decorators, decorators which take arguments, decorator factories and more. Or follow us on twitter.