Closed2

ロバストPython

Akira OtakaAkira Otaka

p150

17.3.1.1 デコレータ

import functools
from collections.abc import Callable

def repeat(times: int = 1) -> Callable:
    """ラップされた関数を指定された回数だけ呼び出す関数"""
    def _repeat(func: Callable):
        @functools.wraps(func)
        def _wrapper(*args, **kwargs):
            for _ in range(times):
                func(*args, **kwargs)
        return _wrapper
    return _repeat

@repeat(times=3)
def say_hello():
    print("Hello")
>>> say_hello()
Hello
Hello
Hello
Akira OtakaAkira Otaka

p307

21.2.1.3 Act(実行)

テーブル駆動

@pytest.mark.parametrize(
    "extra_ingredients,dish_name,expected_calories",
    [
        (["ベーコン", 2400], "ベーコンチーズバーガー", 900),
        ([], "コブサラダ", 1000),
        ([], "鶏手羽先", 800),
        ([], "芽キャベツのニンニク炒め", 200),
        ([], "マッシュポテト", 400),
    ]
)
このスクラップは2023/10/09にクローズされました