question is when i import a dict , i think it is a global var, but python seem create a new dict,they have different id. i can not understand
there are two file named main.py and add_cache.py
# main.py
d = {}
if __name__ == '__main__':
from add_cache import AddDictTest
AddDictTest.add_to_dict("a")
AddDictTest.add_to_dict("b")
print("d id is ", id(d))
print(d)
#add_cache.py
class AddDictTest(object):
@classmethod
def add_to_dict(cls, key):
from main import d
tt = d.setdefault(key, {})
tt["b"] = "b"
print("d id is ", id(d))
when i run main.py
#output is
d id is 2175240751296
d id is 2175240751296
d id is 2175238904384
{}
in my understand , d is not be empty, and there id should same.
I want to konw why create a new empty dict when import , I have ask gpt, it give a answer about cycle import. but i still can not understand deep reason.
Bubble is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.