我正在学习Python并正在阅读一个示例脚本,其中包含一些看起来像的变量定义:
output,_ = call_command('git status')
output,_ = call_command('pwd')
def call_command(command):
process = subprocess.Popen(command.split(' '),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
return process.communicate()
Run Code Online (Sandbox Code Playgroud)
如果我打印输出,我得到结果的shell输出,所以我知道它连接变量.但我在任何文档中都找不到任何对_,convention的引用.有人可以向我解释,以便我确定我正确使用它吗?
Gre*_*ill 13
一般形式
a, b = x, y
Run Code Online (Sandbox Code Playgroud)
是元组赋值.分配了相应的部分,因此上述内容相当于:
a = x
b = y
Run Code Online (Sandbox Code Playgroud)
在您的情况下,call_command()返回两个元素的元组(返回的元素process.communicate()).您将第一个分配给output第二个_(实际上是变量名称,通常用于在不关心值时命名).
| 归档时间: |
|
| 查看次数: |
1390 次 |
| 最近记录: |