<Python, flask> デコっちの応用、、、

みげるっちの講義のFlask-Loginの使われ方をみて、いまいちよくわからんとこがあった。

blog.miguelgrinberg.com

@lm.user_loader
def load_user(id):
    return User.query.get(int(id))

load_user()を他で呼んでいるところもない。

ちとflask_loginのソースを見てみた。

    def user_loader(self, callback):
        '''
        This sets the callback for reloading a user from the session. The
        function you set should take a user ID (a ``unicode``) and return a
        user object, or ``None`` if the user does not exist.

        :param callback: The callback for retrieving a user object.
        :type callback: unicode
        '''
        self.user_callback = callback
        return callback

なんじゃ、こりゃ???
self.user_callbackに引数を保存しているだけ???

試してみた。
まずは、クラス+デコ作成。

In [183]: class deco():
     ...:     def __init__(self):
     ...:         self.x = None
     ...:     def get(self, x):
     ...:         self.x = x
     ...:         return x
     ...:     

In [192]: D = deco()

In [193]: D.x

アトリビュートxは空。
で、デコってみる。

In [195]: @D.get
     ...: def aaa(x):
     ...:     return x * 2
     ...: 

In [196]: aaa(5)
Out[196]: 10

よし、でxを確認。

In [197]: D.x
Out[197]: <function __main__.aaa>

In [198]: D.x(5)
Out[198]: 10

なるほどね~、、、
デコは、こういう使い方もあるのか、、、