0
1

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.

デコレータを使用してみる mshmura

Last updated at Posted at 2021-12-08

目的

  • デコレータの使用に慣れること

内容

関数実行前にメンバーかどうかを判断し、メッセージを切り替える。

login_decorator.py
member_list = ["高橋","田中","井上"]

def check_decorator(func):
    def check_member(name):
        print(f"==={name}さんの会員チェック開始===")
        if name in member_list:
            func(name)
        else:
            print(f"{name}さん、会員登録してください")
        print(f"==={name}さんの会員チェック終了===")
    return check_member
    
@check_decorator
def greeting(name):
    print(f"{name}さん、ようこそ")

# 関数実行
greeting("田中")
greeting("佐々木")

実行結果

===田中さんの会員チェック開始===
田中さん、ようこそ
===田中さんの会員チェック終了===  
===佐々木さんの会員チェック開始===
佐々木さん、会員登録してください  
===佐々木さんの会員チェック終了===

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?