Scaricare Complete Python Developer In | 2020: Zero To Mastery Lezioni [upd]
def retry(max_attempts=3, delay=1, backoff=2): def decorator(func): @wraps(func) def wrapper(*args, **kwargs): attempts = 0 current_delay = delay while attempts < max_attempts: try: return func(*args, **kwargs) except Exception as e: attempts += 1 if attempts == max_attempts: raise print(f"Attempt {attempts} failed: {e}. Retrying in {current_delay}s") time.sleep(current_delay) current_delay *= backoff return wrapper return decorator
def __call__(self, *args, **kwargs): self.calls += 1 print(f"Call {self.calls} of {self.func.__name__}") return self.func(*args, **kwargs) @CountCalls def square(x): return x * x max_attempts: try: return func(*args
multiply(4, 7) from functools import wraps max_attempts: try: return func(*args
@repeat(times=3) def say_hi(): print("Hi!") max_attempts: try: return func(*args