1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

変数名をチェックして挙動を変えるデコレータ

Posted at

あまり実用性はないかも(自分の練習+メモ代わり)

def deco(func):
    from inspect import getargspec
    def inner(*args, **kwargs):
        arg = getargspec(func)
        if 'a' in arg.args:
            print args[arg.args.index('a')];
        return func(*args, **kwargs)
    return inner

このサンプルがやってることは、args引数内に、'a'という名前の引数が含まれていたらその値を表示するという処理。
ある程度メソッドのシグネチャが似ている場合に、特定の形のメソッドならデコレータを適用するとか?
inspectの練習代わりに作成。

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?