Decorators run before function it is decorating is called?

后端 未结 4 1822
逝去的感伤
逝去的感伤 2020-12-06 02:13

As an example:

def get_booking(f=None):
    print "Calling get_booking Decorator"
    def wrapper(request, **kwargs):
        booking = _get_booking         


        
4条回答
  •  一生所求
    2020-12-06 02:38

    python decorators are functions applied to a function to transform it:

    @my_decorator
    def function (): ...
    

    is like doing this:

    def function():...
    function = my_decorator(function)
    

    What you want to do is:

    def get_booking(f=None):
        def wrapper(request, **kwargs):
            print "Calling get_booking Decorator"
            booking = _get_booking_from_session(request)
            if booking == None:
                # we don't have a booking in our session.
                return HttpRedirect('/')
            else:
                return f(request=request, booking=booking, **kwargs)
        return wrapper
    

提交回复
热议问题