Why remains this imported dict empty?

Question:

In this code, I’m importing a dictionnary (dict_nombre) from another Python file(dictnombre.py).

The imported dictionnary is passed in the variable "dict".

Later the code is deleting the key and values of "dict".
Until here, everything is working as expected.

But when I click after that on the button "bouton_chiffre", "dict" remains empty although the function should pass again the imported dictionnary to "dict"

dict_nombre in the file dictnombre.py:

dict_nombre={'un (chiffre)':'yi-',
'deux':'èr',
'trois':'sa-n',
'quatre':'sì',
'cinq':'wû',
'six':'liù',
'sept':'qî',
'huit':'ba-',
'neuf (chiffre)':'jiû',
'dix':'shí'
}

The code:

...
import dictnombre
...

def nombres():
    global dict
    dict=dictnombre.dict_nombre
...
    del dict[key_aleatoire]
...
bouton_chiffre=Button(cadre_bouton,text="Nombres seulement",width=15,height=1,bg="blue",bd=5, command=nombres)
bouton_chiffre.pack(side=LEFT)

SOLUTION (29.03.2023):

dict_copy=dictnombre.dict_nombre.copy()

Thanks everyone for the help !

Asked By: Meriole

||

Answers:

In your code, dict and dictnombre.dict_nombre refer to the exact same object. so when you remove a key from dict it is removing it from the same dictionary that dictnombre.dict_nombre is referencing. That is why once you delete, its gone for good.

Answered By: LhasaDad
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.