Decorators run before function it is decorating is called?

后端 未结 4 1830
逝去的感伤
逝去的感伤 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:16

    I believe python decorators are just syntactic sugar.

    @foo
    def bar ():
        pass
    

    is the same thing as

    def bar ():
        pass
    bar = foo(bar)
    

    As you can see, foo is being called even though bar has not been called. This is why you see the output from your decorator function. Your output should contain a single line for every function you applied your decorator to.

提交回复
热议问题