다음 코드에서 어떤 일이 일어나는지 설명 하시겠습니까?"import my_module"과 "from my_module import my_str"의 차이점을 설명하는 방법은 무엇입니까?
my_module.py :
my_dict = { "key": "default value" }
my_str = "default"
파이썬 인터프리터 :
>>> from my_module import my_dict, my_str
>>> import my_module
>>>
>>> assert my_dict == { "key": "default value" }
>>> assert my_module.my_dict == { "key": "default value" }
>>> assert my_str == "default string"
>>> assert my_module.my_str == "default string"
>>>
>>> my_dict["key"] = "new value"
>>> my_str = "new string"
>>>
>>> assert my_dict == { "key": "new value" }
>>> assert my_module.my_dict == { "key": "new value" } # why did this change?
>>> assert my_str == "new string"
>>> assert my_module.my_str == "default string" # why didn't this change?
>>>
>>> my_module.my_dict["key"] = "new value 2"
>>> my_module.my_str = "new string 2"
>>>
>>> assert my_dict == { "key": "new value 2" } # why did this change?
>>> assert my_module.my_dict == { "key": "new value 2" }
>>> assert my_str == "new string" # why didn't this change?
>>> assert my_module.my_str == "new string 2"
>>>
>>> my_dict = { "new_dict key" : "new_dict value" }
>>> assert my_dict == { "new_dict key": "new_dict value" }
>>> assert my_module.my_dict == { "key": "new value 2" } # why didn't this change?