Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Challenge: Logging Decorator | Built-in and Real-World Decorators
Quizzes & Challenges
Quizzes
Challenges
/
Python Decorators Explained

bookChallenge: Logging Decorator

As you have seen in the previous chapter, logging is a practical and common use case for decorators in Python. By wrapping a function with a logging decorator, you can automatically record when the function is called, along with its arguments. This approach keeps your code tidy and avoids repetitive logging statements inside every function. Logging decorators are especially useful for debugging, monitoring, or auditing function usage in real-world applications.

Tarea

Swipe to start coding

Write a decorator named log_calls that logs the function name and all arguments every time the decorated function is called. The decorator should print the function name followed by the positional and keyword arguments in a readable format. The original function should be called with the provided arguments, and its return value should be returned unchanged.

  • The log should use the format: Calling <function_name> with args=<args>, kwargs=<kwargs>.
  • The arguments should be shown as Python would display them (tuples and dicts).
  • The decorator must not change the return value of the decorated function.

Solución

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 3
single

single

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Suggested prompts:

Can you show me an example of a logging decorator in Python?

What are some best practices for using logging decorators?

How can I customize the information that gets logged by the decorator?

close

bookChallenge: Logging Decorator

Desliza para mostrar el menú

As you have seen in the previous chapter, logging is a practical and common use case for decorators in Python. By wrapping a function with a logging decorator, you can automatically record when the function is called, along with its arguments. This approach keeps your code tidy and avoids repetitive logging statements inside every function. Logging decorators are especially useful for debugging, monitoring, or auditing function usage in real-world applications.

Tarea

Swipe to start coding

Write a decorator named log_calls that logs the function name and all arguments every time the decorated function is called. The decorator should print the function name followed by the positional and keyword arguments in a readable format. The original function should be called with the provided arguments, and its return value should be returned unchanged.

  • The log should use the format: Calling <function_name> with args=<args>, kwargs=<kwargs>.
  • The arguments should be shown as Python would display them (tuples and dicts).
  • The decorator must not change the return value of the decorated function.

Solución

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 3
single

single

some-alt