Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python礼仪:导入模块_Python_Coding Style - Fatal编程技术网

Python礼仪:导入模块

Python礼仪:导入模块,python,coding-style,Python,Coding Style,假设我有两个Python模块: module1.py: import module2 def myFunct(): print "called from module1" def myFunct(): print "called from module2" def someFunct(): print "also called from module2" import module1 module1.myFunct() # prints "called from module1" modul

假设我有两个Python模块:

module1.py

import module2
def myFunct(): print "called from module1"
def myFunct(): print "called from module2"
def someFunct(): print "also called from module2"
import module1
module1.myFunct() # prints "called from module1"
module1.module2.myFunct() # prints "called from module2"
 import module1, module2
 module1.myFunct() # prints "called from module1"
 module2.myFunct() # prints "called from module2"
module2.py

import module2
def myFunct(): print "called from module1"
def myFunct(): print "called from module2"
def someFunct(): print "also called from module2"
import module1
module1.myFunct() # prints "called from module1"
module1.module2.myFunct() # prints "called from module2"
 import module1, module2
 module1.myFunct() # prints "called from module1"
 module2.myFunct() # prints "called from module2"
如果我导入
module1
,重新导入
module2
,或者将其称为
module1.module2
,这是更好的礼节吗

例如(
someotherfile.py
):

我也可以这样做:
module2=module1.module2
。现在,我可以直接调用
module2.myFunct()

但是,我可以将
module1.py
更改为:

from module2 import *
def myFunct(): print "called from module1"
现在,在
someotherfile.py
中,我可以执行以下操作:

import module1
module1.myFunct() # prints "called from module1"; overrides module2
module1.someFunct() # prints "also called from module2"
另外,通过导入
*
,帮助(“module1”)显示
模块2
中的所有函数

另一方面,(假设
module1.py
使用
import module2
),我可以:
someotherfile.py

import module2
def myFunct(): print "called from module1"
def myFunct(): print "called from module2"
def someFunct(): print "also called from module2"
import module1
module1.myFunct() # prints "called from module1"
module1.module2.myFunct() # prints "called from module2"
 import module1, module2
 module1.myFunct() # prints "called from module1"
 module2.myFunct() # prints "called from module2"
再说一次,哪种礼仪和实践更好?要再次导入
模块2
,或仅参考
模块1的导入?

引用以下内容:

从包含模块的类导入类时,通常可以拼写如下:

from myclass import MyClass
from foo.bar.yourclass import YourClass
如果此拼写导致本地名称冲突,请拼写它们

import myclass
import foo.bar.yourclass
我的

不要使用
模块1.module2
;您依赖的是
模块1
的内部实现细节,这可能会在以后更改它使用的导入内容。您可以直接导入
module2
,除非模块作者另有说明,否则请这样做

您可以使用从modulename import*
,限制从模块导入的内容;
help()
命令也尊重该列表。列出您在
\uuuu all\uuuuu
中显式导出的名称有助于清理
帮助()
文本演示文稿:

通过检查模块名称空间中名为
\uuuuu all\uuuuuu
的变量来确定模块定义的公共名称;如果已定义,则它必须是由该模块定义或导入的名称组成的字符串序列。
\uuuuuu all\uuuuu
中给出的名称都被视为公共名称,并且必须存在。如果未定义
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
,则公用名称集包括模块<代码>\uuuuuuuuuuuuuuuuuuuuu应包含整个公共API。其目的是避免意外导出不属于API的项(例如导入并在模块中使用的库模块)


只需导入模块2。重新导入的成本相对较低,因为Python将模块对象缓存在内存中

此外,在
module1.module2.myFunct
中链接点违反了。也许有一天你会想用另一个模块
module1a
替换
module1
,它不导入
module2
。通过使用
import module2
,您将避免重写所有出现的
module1.module2.myFunct

来自module2 import*的
通常是一种不好的做法,因为这样很难跟踪变量的来源。混合模块名称空间可能会产生变量名称冲突。例如,numpy import*
中的
是一个明确的no-no,因为这样做会覆盖Python的内置
sum
min
max
any
all
abs
round