0

I am explicitly studying the following decorator

#decorators.py
from django.core.urlresolvers import reverse 
from django.http import HttpResponseRedirect

def auth_check(view_func): 
    def _wrapped_view_func(request, *args, **kwargs): 
        if request.user.is_authenticated(): 
            return HttpResponseRedirect(reverse('pets:home')) 
        return view_func(request, *args, **kwargs) 
    return _wrapped_view_func

I few a few questions regarding this decorator and decorators in general.

  1. How do they know to only be applied to the code directly below them? Also, it is usually the case that code will get an error if using parameters that aren't defined yet (code underneath in the script)

  2. What exactly is *args and **kwargs doing? I realize they stand for argument variables, but why do you need two of them? Would you need one for each possible variable? How does this work?

  3. Why are we "wrapping" the view function? I know thats just a name, but why is it called a wrapper in this case? Is this usually how its done?

ApathyBear
  • 9,057
  • 14
  • 56
  • 90
  • These questions are not really about decorators at all, but some fundamental Python concepts like variable scoping. – Daniel Roseman May 16 '14 at 18:21
  • Good questions - I've marked this as a duplicate - if the answers in the linked question don't fully answer your question, please edit this question or ask a new one :-) – Sean Vieira May 16 '14 at 18:21

0 Answers0