I have always known that decorators can enhance an existing method. Python also provides an annotation method, which is very useful. But when we look at the flask login extension package, we find that decorators can also implement the registration function of callback functions.
Flask login is to register the callback function through the following decorator. When there is no sessionID, the function specified by the decorator is used to read the user into the session.
@login_manager.user_loader
A simple test example is written below to demonstrate this functionality.
import time import functools class Test(): #/**feature will call callback(), but callback is not really defined in Test**/ def feature(self): self.callback() def decorate(self, func): self.callback=func return func test = Test() #/**Register foo as a callback function*// @test.decorate def foo(): print 'in foo()' #/**Calling the feature will trigger the callback function**/ test.feature()