[Python] How do I modify a global dictionary inside a Python function? [closed]

Discussão em 'Python' iniciado por Stack, Novembro 20, 2024.

  1. Stack

    Stack Membro Participativo

    I'm learning Python and trying to modify a global dictionary inside a function. Here's the code I have: enter image description here

    I expect the output to be {'1': 'd'}, but instead, the output is {}. I want to know how to properly modify the global dictionary a inside the function so that it holds the value {'1': 'd'}.

    • I used global a inside the function to modify the global variable.
    • I also tried **copy.deepcopy() **to avoid unintentional changes to the original dictionary.

    Continue reading...

Compartilhe esta Página