1. 程式人生 > >回撥傳遞方法和注入例項

回撥傳遞方法和注入例項

注入例項

#conding=utf-8
class FlaskLocalStack(object):
    local = None

class LoginManager(object):
    def __init__(self):
        # print "in __init__"
        pass
    def init_app(self, app):
        # print id(self)
        app.local = self
    def login(self):
        print "login successful"
lg = LoginManager()
ll = FlaskLocalStack()
lg.init_app(ll)    
ll.local.login()

回撥傳遞方法

class LoginManager(object):
    def __init__(self):
        pass
    def user_loader(self, callback):
        self.user_callback = callback
        return callback
import login_manager

@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))

執行上下文中login_manager單例