python - Using __call__ to implement decorator functions
某草草
某草草 2017-06-28 09:24:02
0
1
711

Use __call__ of a class to implement a decorator, as shown in the following code



class Check(object):
    def __init__(self, name):
        self.name = name

    def __call__(self, func):

        print "1111111111"

        def _decorator(*args, **kwargs):
            print "2222222222"
            return func(*args, **kwargs)
          

If you use this class as a decorator to decorate a function.

@Check("param")
def param_check(request):
    "python code....."
    return Response("ok")

Here comes the problem. Start a django or any other python process. When executing this param_check route, print "1111111111" is only printed for the first time, and 1111111111111 is not printed when called later.
And print "2222222222" is printed every time. My understanding is that __call__ is defining this decorator for the first time, so it is only executed once. It has been defined when requested again later, and only the function body part is executed, which is the print 222222222 part. Who knows the features of this python decorator

某草草
某草草

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!