Python 术语:“= argv"左边的东西在 Learn Python the Hard Way 练习 13 [英] Python terminology: things to left of "= argv" in Learn Python the Hard Way exercise 13

查看:30
本文介绍了Python 术语:“= argv"左边的东西在 Learn Python the Hard Way 练习 13的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Zed Shaw 的Learn Python the Hard Way"经常要求您用英语写出"脚本的每一行都做了什么.我正在努力使用与函数(命令?)argv 相关的一些东西来做到这一点,因为我不知道如何命名代码的某些部分.哎呀,我什至不知道该怎么称呼 argv——一个函数?命令?多变的?我知道这是一个模块.但回到正轨:

Zed Shaw's "Learn Python the Hard Way" frequently asks you to "write out in English" what each and every line of a script does. I am struggling to do that with some stuff associated with the function (command?) argv because I don't know what to name certain parts of the code. Heck, I don't even know what to call argv--a function? A command? Variable? I know it's a module. But back on track:

这是练习 13 中的代码:

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

Zed 声明'argv' 是参数变量".我的问题是,第三行等号左边的东西的名称是什么?

Zed states "The 'argv' is the "argument variable." My question is, what is the name of the things to the left of the equals sign on line three?

通俗地说,我的冲动是将脚本"、第一"等词称为变量本身——但这似乎不对,因为根据 Zed argv 是参数变量".

Colloquially speaking, my urge is to call the words "script," "first" etc. variables themselves--but that doesn't seem right, since according to Zed argv is "the argument variable."

我也不认为称它们为参数"是正确的;我只阅读了命令行参数方面的参数".

I did not think calling them "arguments" is correct either; I've only read "argument" in terms of command line arguments.

参数"似乎很可能,因为它在练习标题中提到过,但是使用等号"、="、python"、argv"、定义"等组合进行网络搜索不是很有启发性.当你不知道它们叫什么时,搜索是非常困难的.

"Parameters" seemed likely, since it was mentioned in the title of the exercise, but doing web searches with combinations of "equals sign," "=," "python," "argv," "definition" and so on wasn't very enlightening. Searching for things is very difficult when you don't know what they're called.

我非常清楚此脚本中发生的事情,只是不清楚其中一部分的名称.我很确定当我回答这个问题时我会拍自己的额头.

I am very clear on what's happening in this script, I'm just not clear on the name of a part of it. I am very sure I'm going to slap my forehead when this is answered.

推荐答案

"=" 左边的东西是变量,它们得到它们的 来自右侧的变量.

The things to the left of the "=" are variables that get their value from the variable on the right.

给定:

script, first, second, third = argv

argv 是一个 list 字符串,在本例中包含 4 个项目.这些字符串被解包"并分配给 = 左侧的四个变量.

argv is a list of strings which in this case contains 4 items. These strings are "unpacked" and assigned to the four variables on the left of the =.

argv 在从命令行调用 Python 程序时获取其值,如下所示:

argv gets its value is when a Python program is invoked from the command line, like this:

test.py this is sure cool

在这种情况下 argv 将包含 ['test.py', 'this', 'is', 'sure', 'cool'].命令后面的这些字符串称为命令行参数"(请参阅​​此教程) 和脚本名称,任何参数都存储在 argv 中.这是在启动脚本时向脚本发送信息的一种方式.

in this case argv will contain ['test.py', 'this', 'is', 'sure', 'cool']. These strings after the command are called "command line arguments" (see this tutorial) and the name of the script, and any arguments are stored in argv. This is a way to send information to the script when you start it.

在这种情况下,变量获得以下值:

In this case the variables get the following values:

  script is set to  "this.py"  # the string is in argv[0]
  first to "is"     # argv[1]
  second to "sure"  # argv[2]

  third to "cool"   # argv[3]

所以:

  script, first, second, third = argv

实际上相当于:

  script = argv[0]
  first = argv[1]
  second = argv[2]
  third = argv[3]

只是 Python 可以让您一举完成这项任务.

It's only that Python lets you do this assignment in one nice swoop.

请注意,您可以使用适当的索引值以任何顺序提取命令行参数.

Note that you can pull out your command line arguments in any order using the appropriate index value.

此机制用于将信息传递给 Python 脚本.您可以想象运行一个需要输入文件和输出文件的程序.您可以在命令行中提供它们,而不是在脚本中对它们进行硬编码.例如,

This mechanism is used to communicate information the to the Python script. You can imagine running a program that expects an input file and and output file. Instead of hardcoding them in your script, you could provide them on the command line. E.g.,

 computeData.py input.txt result.txt

这篇关于Python 术语:“= argv"左边的东西在 Learn Python the Hard Way 练习 13的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆