I am learning Python and am reading through an example script that includes some variable definitions that look like:
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()
If I print output I get the resulting shell output strung together, so I know it’s concatenating the variables. But I can’t find any reference to the ,_ convention in any of the docs. Can someone explain it to me so that I know for sure I am using it correctly?
The general form
is tuple assignment. The corresponding parts are assigned, so the above is equivalent to:
In your case,
call_command()returns a tuple of two elements (which is whatprocess.communicate()returns). You’re assigning the first one tooutputand the second one to_(which is actually a variable name, typically used to name something when you don’t care about the value).