0
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 3 years have passed since last update.

デコレータ

Last updated at Posted at 2020-02-14
decorator.py
def print_info(func):
    def wrapper(*args, **kwargs):
        print('start')
        result = func(*args,**kwargs)
        print('end')
        return result
    return wrapper

#関数に何かを付け加える場合に便利(デコレーターを複数適用する場合は順番に注意)
@print_info
def add_num(a, b):
    return a+b

r = add_num(10,20)
print(r)
#デコレータを使わない場合、下記のように書けるが分かりにくい
#f = print_info(add_num)
#r = f(10,20)
#print(r)

出力:

start
end
30
0
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
0
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?