猫型エンジニアのブログ

プログラム/ネットワーク系の技術関連をまとめたページです 

デコレータ その3

 デコレータに引数がある場合の処理です。以下のようにデコレータの引数を処理するために、ネストをもう一段深くします。

実効結果からデコレータの引数の?が末尾に追加されているのが見て取れます。

#deco4.py
def print_is_a_(arg):
    def _add_string(function):
        def __add_string():
            function()
            print "is a programing language", arg
        return __add_string
    return _add_string

@print_is_a_("?")
def print_python():
    print "Python"

print_python()

実効結果

# python deco6.py 
Python
is a programing language ?