#!/usr/bin/python3 # -*- coding: utf-8 -*- # %% functools (optional topic) # In this example `beg` wraps `say`. # If say_please is True then it, then # will change the returned message. from functools import wraps from typing import Tuple help(wraps) def beg(target_function): @wraps(target_function) def wrapper(*args, **kwargs): msg, say_please = target_function(*args, **kwargs) if say_please: return "{} {}".format(msg, "Please! I am poor :(") return msg return wrapper @beg def say(say_please: bool = False) -> Tuple[str, bool]: msg = "Can you buy me a meal?" return msg, say_please print(say()) # Can you buy me a meal? print(say(say_please=True)) # Can you buy me a meal? Please! I am poor :(