笨方法学python之import sys与from sys import argv的区别

这是在网上看到的一个大神的解答: 

sys is a module that contains “system functionality”. sys.argv is a list containing your script’s command line arguments. One way to use it would be to write import sys and then sys.argv to access it.

from module import names is an alternative way to import a module that allows you to access the given names without naming the module. That is writing from sys import argv allows you to just write argv whereas import sys would require you to write sys.argv instead.

翻译如下:sys是一个模块,里面包含一些系统函数,sys.list是一个列表,其中包含你的脚本想运行的一些命令行参数,使用他的一个方法就是书写:sys.argv。

from module import names是一种变相导入模块的方法,允许你直接使用变量名(names)而不需要导入模块名。from sys import argv这种方式可以允许你直接使用argv,而不需要再这样sys.argv书写。

下面是自己的理解: 
import sys 把sys模块包含的所有函数和参数不管你需不需要,统统包含进来,就好比C语言中的#include()指令 
from sys import argv 导入sys中的argv参数,并不会将sys模块中的所有函数和变量包含进来,只会导入argv变量,这也就是所谓的让你的程序保持精简。脚本中使用到argv参数时,就会调用sys中的argv参数。

猜你喜欢

转载自www.cnblogs.com/Jzeng666/p/9589547.html
今日推荐