How does this python decorator work?

前端 未结 3 1083
孤街浪徒
孤街浪徒 2021-01-15 04:59

Edit/Clarification to make my question specific to my query: *I can see how the decorator static log function is called but I don\'t see how _ is

3条回答
  •  旧时难觅i
    2021-01-15 05:46

    def decorate(fn):
        def wrapper():
            fn()
        print 'Wrapping function ', wrapper
        return wrapper
    
    def x():
        pass
    
    print 'Original x ', x
    
    x = decorate(x)
    
    print 'New x ', x
    

    Output:

    Original x 
    Wrapping function 
    New x  
    

    Note how x is now the same as wrapper. When you call x, you are actually calling wrapper.

    Do not mark this answer as the answer. Mark one of the others as the answer. The point of this is to correct a misunderstanding so that you may then correctly understand one of the other answers.

提交回复
热议问题