LoginSignup
0
0

More than 5 years have passed since last update.

デコレータ

Posted at

@hoge
みたいなやつ

def poko(func):
    print "poko"
    return "poko_string"

@poko
def hoge():
    pass

def hoge():
    pass
hoge = poko(hoge)

と同じ。
実際はfunctoolのwraps関数を使ったり

from functools import wraps

def poko(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        """ 加えたい動作 """"
        # do pokopoko
        # add hogehoge とか
        return func(*args, **kwargs)
    return wrapper

@poko
def hoge():
    pass

みたいに

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0