笨方法学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參數。
總結
以上是生活随笔為你收集整理的笨方法学python之import sys与from sys import argv的区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C:\Python27\python.e
- 下一篇: python argv参数