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

本文详细介绍了Python中的sys模块,解释了sys.argv的功能及其使用方法。sys.argv是一个列表,用于存储脚本的命令行参数;通过import sys或from sys import argv的方式引入,前者需要通过sys.argv调用,后者则可以直接使用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参数。

转载于:https://www.cnblogs.com/Jzeng666/p/9589547.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值