1. 程式人生 > >笨方法學python之import sys與from sys import argv的區別

笨方法學python之import sys與from sys import argv的區別

use 直接 所有 pop 本想 write div 一個 ng-

這是在網上看到的一個大神的解答:

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參數。

笨方法學python之import sys與from sys import argv的區別