デコレータで関数に属性を付与
def note(**kw): def applier(func): __notes__ = getattr(func, '__notes__', None) if __notes__ is None: __notes__ = dict() setattr(func, '__notes__', __notes__) __notes__.update(kw) return func return applier @note(foo='this is add') @note(bar='加算') def add(a, b): return a + b print('add(10, 20) ->', add(10, 20)) print('add.__notes__ ->', add.__notes__)
$ python hoge.py add(10, 20) -> 30 add.__notes__ -> {'bar': '加算', 'foo': 'this is add'}