 December 17, 2011 at 18:59
 December 17, 2011 at 18:59How in python outside to check the values passed to a function
A small HowTo for beginner python programmers on how to check some values passed into a function from the outside. Actually, I needed it in Django , but there is nothing specific for the framework and this ... 
UPD Lyrical digression: Initially, I wrote a lot of stupid things because of my crooked understanding of the work of decorators. Now the decision is much more correct and direct. Moreover, I can even clearly explain it, for which I am very grateful to the commentators.
To begin, I recommend that you familiarize yourself with the work of decorators (very carefully!).
The problem I had was literally the following:
There is some kind of view function for Djangobefore which you need to check if there are some variables in the user profile.
The specific solution is:
The function
The principle of the decorator:
Those. in fact, instead of the original function, the function
Thanks again to Deepwalker for the clarification.
UPD Lyrical digression: Initially, I wrote a lot of stupid things because of my crooked understanding of the work of decorators. Now the decision is much more correct and direct. Moreover, I can even clearly explain it, for which I am very grateful to the commentators.
To begin, I recommend that you familiarize yourself with the work of decorators (very carefully!).
The problem I had was literally the following:
There is some kind of view function for Djangobefore which you need to check if there are some variables in the user profile.
The specific solution is:
def check_nickname(funct):
    def wrapper(request, *args, **kwargs):
        if request.user.profile.nickname:
            return funct(request, *args, **kwargs)
        else:
            from django.shortcuts import render_to_response
            from django.template import RequestContext
            return render_to_response('need_profile.html', RequestContext(request))
    return wrapperThe function
check_nicknameis a decorator to a function of the form Django, about which it is known for certain that a parameter of a requestcertain type is passed to it . The decorated function is passed to wrapperwhich and is returned instead of the decorated function. The principle of the decorator:
@f1
def func(x): pass
#эквивалентно этому:
def func(x): pass
func = f1(func)
Those. in fact, instead of the original function, the function
wrapperreturned by the decorator is  check_nicknamecalled. And it wrapperreceives all the parameters intended for the original function of the view, and the logic of the following actions is already being built on their basis. Thanks again to Deepwalker for the clarification.