简体   繁体   English

如何解释“ import my_module”和“ from my_module import my_str”之间的区别?

[英]How to explain difference between “import my_module” and “from my_module import my_str”?

How would you describe what is going on in the following code? 您如何描述以下代码中发生的事情?

my_module.py : my_module.py:

my_dict = { "key": "default value" }
my_str = "default"

Python interpreter: Python解释器:

>>> 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?

my_module.my_str refers to the my_str object in the my_module namespace, and modifying my_module.my_str modifies that object. my_module.my_str引用my_module命名空间中的my_str对象,修改my_module.my_str会修改该对象。

from my_module import my_str creates a my_str object in the local namespace, copied from the my_module.my_str object. from my_module import my_str在本地名称空间中创建一个my_str对象,该对象是从my_module.my_str对象复制的。 Modifying my_str modifies the object in the local namespace, but does not modify the my_module.my_str object. 修改my_str会修改本地名称空间中的对象,但不会修改my_module.my_str对象。

my_dict is a reference in the local namespace to my_module.my_dict . my_dict是本地名称空间中对my_module.my_dict的引用。 Because it's a reference to a mutable object (a dictionary), changes made to my_dict affect my_module.my_dict and vice versa. 因为它是对可变对象(字典)的引用,所以对my_dict所做的更改会影响my_module.my_dict ,反之亦然。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何导入自己的模块进行模拟? (导入错误:没有名为my_module的模块!) - How to import own module for mocking? (import error: no module named my_module!) 知道filename:line_no导入到my_module的位置 - know filename:line_no where an import to my_module was made ModuleNotFoundError:没有名为“my_module”的模块 - ModuleNotFoundError: No module named 'my_module' `pickle`:另一个`ImportError:没有名为my_module的模块。 - `pickle`: yet another `ImportError: No module named my_module` (又一个)“导入错误:没有名为 my_module 的模块” - (Yet Another) 'ImportError: No module named my_module' 出现错误 ModuleNotFoundError: No module named 'my_module' - Getting error ModuleNotFoundError: No module named 'my_module' Python - 在终端中“没有名为 my_module 的模块”,但在 PyCharm 中没有 - Python - “No module named my_module” in terminal, but not in PyCharm setuptools何时安装到/…/ my_module / build / lib /的可编辑链接,何时链接到/…/ my_module? - When does setuptools install editable link to /…/my_module/build/lib/ andwhen does it link to /…/my_module? 如何从我的模块导入和创建我的类? - How to import and create my class from my module? 为什么我的字符串反转 my_str[len(my_str)-1:-1:-1] 不产生 output? - Why doesn't my string reversal my_str[len(my_str)-1:-1:-1] produce output?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM