2024-04-25 08:11:30 发布
网友
很抱歉提出这个可能很幼稚的问题。我试着找doc做些实验,但我想确定是这样的:
如果,在文件中测试.py,我有:
import module1
我在控制台里这样做:
import test
我不会在控制台中导入模块1。你知道吗
如果我这么做:
from test import *
此外,模块1不会被导入控制台。你知道吗
对吗? 谢谢!你知道吗
你的实验可以很容易地从外壳上进行:
╭─phillip@phillip-laptop ~ ‹ruby-1.9.3@global› ‹pandas› ╰─$ echo "import module1" > test.py ╭─phillip@phillip-laptop ~ ‹ruby-1.9.3@global› ‹pandas› ╰─$ touch module1.py ╭─phillip@phillip-laptop ~ ‹ruby-1.9.3@global› ‹pandas› ╰─$ py Python 2.7.5 (default, May 17 2013, 07:55:04) [GCC 4.8.0 20130502 (prerelease)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import test >>> test.module1 <module 'module1' from 'module1.py'> >>> from test import * >>> module1 <module 'module1' from 'module1.py'>
这只会将名称test导入当前命名空间。test命名空间中的任何内容都可以作为test.whatever访问;特别是,module1可以作为test.module1访问,尽管您不应该使用它。你知道吗
test
test.whatever
module1
test.module1
这会将所有不以下划线开头的内容从test的命名空间导入当前命名空间。由于module1在test的名称空间中可用,因此这将导入名称module1。你知道吗
你的实验可以很容易地从外壳上进行:
这只会将名称
test
导入当前命名空间。test
命名空间中的任何内容都可以作为test.whatever
访问;特别是,module1
可以作为test.module1
访问,尽管您不应该使用它。你知道吗这会将所有不以下划线开头的内容从
test
的命名空间导入当前命名空间。由于module1
在test
的名称空间中可用,因此这将导入名称module1
。你知道吗相关问题 更多 >
编程相关推荐