习题 13: 参数、解包、变量
在这节练习中,我们将降到另外一种将变量传递给脚本的方法(所谓脚本,就是你写的 .py
程序)。你已经知道,如果要运行 ex13.py
,只要在命令行运行 python ex13.py
就
可以了。这句命令中的 ex13.py
部分就是所谓的“参数(argument)”,我们现在要做的
就是写一个可以接受参数的脚本。
将下面的程序写下来,后面你将看到详细解释。
from sys import argv
script, first, second, third = argv
print "The script is called:", script
print "Your first variable is:", first
print "Your second variable is:", second
print "Your third variable is:", third
在第 1 行我们有一个“import”语句. 这是你将 python 的功能引入你的脚本的方法. Python 不会一下子将它所有的功能给你,而是让你需要什么就调用什么。这样可以让你的程序保持 精简,而后面的程序员看到你的代码的时候,这些“import”可以作为提示,让他们明白你的 代码用到了哪些功能。
argv
是所谓的“参数变量(argument variable)”,是一个非常标准的编程术语。在其他
的编程语言里你也可以看到它。这个变量包含了你传递给 Python 的参数。通过后面的练习
你将对它有更多的了解。
第 3 行将 argv
“解包(unpack)”,与其将所有参数放到同一个变量下面,我们将每个参数
赋予一个变量名: script
, first
, second
, 以及 third
\。这也许看上去
有些奇怪, 不过"解包"可能是最好的描述方式了。它的含义很简单:“把 argv 中的东西解包,
将所有的参数依次赋予左边的变量名”。
接下来就是正常的打印了。
等一下!“功能”还有另外一个名字
前面我们使用 import
让你的程序实现更多的功能,但实际上没人吧 import
称为
“功能”。我希望你可以在没接触到正式术语的时候就弄懂它的功能。在继续下去之前, 你需要
知道它们的真正名称:模组(modules)
。
从现在开始我们将把这些我们 导入(import)
进来的功能称作 模组
。你将看到类似这样
的说法:“你需要把 sys
模组 import 进来。”也有人将它们称作“库(libraries)”,不过
我们还是叫它们模组吧。
What You Should See
Warning 注意!你一直在运行不带命令行参数Python脚本。如果只键入
python ex13.py
,那你就搞错了!注意我是如何运行它的。你看到的所有使用argv的地方都是这样的。
用下面的方法运行你的程序:
$ python ex13.py first 2nd 3rd
The script is called: ex13.py
Your first variable is: first
Your second variable is: 2nd
Your third variable is: 3rd
如果你每次使用不同的参数运行,你将看到下面的结果:
$ python ex13.py stuff things that
The script is called: ex13.py
Your first variable is: stuff
Your second variable is: things
Your third variable is: that
$
$ python ex13.py apple orange grapefruit
The script is called: ex13.py
Your first variable is: apple
Your second variable is: orange
Your third variable is: grapefruit
你其实可以将“first”、“2nd”、“3rd”替换成任意三样东西。你可以将它们换成任意 你想要的东西.
如果你没有运行对,你将看到如下错误:
$ python ex13.py first 2nd
Traceback (most recent call last):
File "ex13.py", line 3, in <module>
script, first, second, third = argv
ValueError: need more than 3 values to unpack
当你运行脚本时提供的参数的个数不对的时候,你就会看到上述错误信息 (这次我只用了
first 2nd
两个参数)。“need more than 3 values to unpack”这个错误信息告诉
你参数数量不足。
Study Drills
- 给你的脚本三个以下的参数。看看会得到什么错误信息。试着解释一下。
- 再写两个脚本,其中一个接受更少的参数,另一个接受更多的参数,在参数解包时给它们 取一些有意义的变量名。
- 将
raw_input
和argv
一起使用,让你的脚本从用户手上得到更多的输入。 - 记住“模组(modules)”为你提供额外功能。多读几遍把这个词记住,因为我们后面\ 还会用到它。
Common Student Questions
When I run it I get ValueError: need more than 1 value to unpack . |
---|
Remember that an important skill is paying attention to details. If you look at the What You Should See section you see that I run the script with parameters on the command line. You should replicate how I ran it exactly. |
What's the difference between argv and raw_input() ? |
The difference has to do with where the user is required to give input. If they give your script inputs on the command line, then you use argv . If you want them to input using the keyboard while the script is running, then use raw_input() . |
Are the command line arguments strings? |
Yes, they come in as strings, even if you typed numbers on the command line. Use int() to convert them just like with int(raw_input()) . |
How do you use the command line? |
You should have learned to use it real quick by now, but if you need to learn it at this stage, then read the Command Line Crash Course I wrote for this book in Appendix A. |
I can't combine argv with raw_input() . |
Don't overthink it. Just slap two lines at the end of this script that uses raw_input() to get something and then print it. From that start playing with more ways to use both in the same script. |
Why can't I do this raw_input('? ') = x ? |
Because that's backward to how it should work. Do it the way I do it and it'll work. |
Copyright (C) 2010 by
Author: Zed Shaw
Translator:Zander Wong