Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Challenge: Default Argument Debugger | Mutable Objects and Function Arguments
Common Python Mistakes and How to Fix Them

bookChallenge: Default Argument Debugger

In Python, mutable default arguments can lead to subtle bugs that are often hard to spot. When you use a mutable object such as a list or dict as a default value for a function parameter, Python only creates that object once—at the time the function is defined, not each time the function is called. This can cause the function to share the same object across multiple calls, resulting in unexpected behavior.

Your task is to examine some functions that use mutable default arguments and refactor them so that each call to the function gets a new object, preventing unintended side effects.

Tarea

Swipe to start coding

Refactor the provided functions to avoid using mutable default arguments. Your goal is to ensure that each call to the function creates a new list or dictionary if none is provided by the caller.

  • Replace any mutable default argument with None.
  • Inside the function, check if the parameter is None and, if so, assign a new empty list or dictionary as appropriate.
  • Ensure that the function's behavior remains unchanged except for the fix.
  • Use the provided string templates for printing output:
    • Current items: {items}
    • Current mapping: {mapping}

Solución

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. 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 function with a mutable default argument?

How should I refactor a function to avoid this issue?

Why does Python behave this way with default arguments?

close

Awesome!

Completion rate improved to 5.26

bookChallenge: Default Argument Debugger

Desliza para mostrar el menú

In Python, mutable default arguments can lead to subtle bugs that are often hard to spot. When you use a mutable object such as a list or dict as a default value for a function parameter, Python only creates that object once—at the time the function is defined, not each time the function is called. This can cause the function to share the same object across multiple calls, resulting in unexpected behavior.

Your task is to examine some functions that use mutable default arguments and refactor them so that each call to the function gets a new object, preventing unintended side effects.

Tarea

Swipe to start coding

Refactor the provided functions to avoid using mutable default arguments. Your goal is to ensure that each call to the function creates a new list or dictionary if none is provided by the caller.

  • Replace any mutable default argument with None.
  • Inside the function, check if the parameter is None and, if so, assign a new empty list or dictionary as appropriate.
  • Ensure that the function's behavior remains unchanged except for the fix.
  • Use the provided string templates for printing output:
    • Current items: {items}
    • Current mapping: {mapping}

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 4. Capítulo 3
single

single

some-alt