Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Challenge: Retry Decorator | Decorator Practice Lab
Python Decorators Explained

bookChallenge: Retry Decorator

You have now seen how decorators can enhance functions with additional behavior, such as logging, timing, or formatting. Sometimes, you may want to make your functions more robust by automatically retrying them when they fail due to exceptions. This is especially useful when dealing with unreliable operations, like network requests or file I/O, where a temporary glitch might cause a function to fail even though retrying would succeed. In this challenge, you will implement a decorator that retries a function up to three times if it raises an exception, and only gives up if all attempts fail.

Tarefa

Swipe to start coding

Write a decorator that retries a function up to three times if it raises an exception, then re-raises the exception if all attempts fail.
The decorator must:

  • Attempt to call the decorated function.
  • If the function raises an exception, catch it and retry up to two more times.
  • If the function still raises an exception after three total attempts, re-raise the last exception.
  • If the function succeeds on any attempt, return its result.

Solução

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 5
single

single

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Suggested prompts:

Can you show me how to implement the retry decorator?

What should the decorator do if the function keeps failing after three attempts?

Can you give an example of using this decorator with a function that might fail?

close

bookChallenge: Retry Decorator

Deslize para mostrar o menu

You have now seen how decorators can enhance functions with additional behavior, such as logging, timing, or formatting. Sometimes, you may want to make your functions more robust by automatically retrying them when they fail due to exceptions. This is especially useful when dealing with unreliable operations, like network requests or file I/O, where a temporary glitch might cause a function to fail even though retrying would succeed. In this challenge, you will implement a decorator that retries a function up to three times if it raises an exception, and only gives up if all attempts fail.

Tarefa

Swipe to start coding

Write a decorator that retries a function up to three times if it raises an exception, then re-raises the exception if all attempts fail.
The decorator must:

  • Attempt to call the decorated function.
  • If the function raises an exception, catch it and retry up to two more times.
  • If the function still raises an exception after three total attempts, re-raise the last exception.
  • If the function succeeds on any attempt, return its result.

Solução

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 5
single

single

some-alt