bryan kennedy
bryan kennedy

Reputation: 7199

What does var,_ = something mean in Python? String concatenation?

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?

Upvotes: 6

Views: 3320

Answers (5)

tantalor
tantalor

Reputation: 119

You see this in perl, too, with undef instead of _.

($dev, $ino, undef, undef, $uid, $gid) = stat($file);

See http://perldoc.perl.org/perldata.html#List-value-constructors

Upvotes: 2

kindall
kindall

Reputation: 184280

_ is a valid variable name in Python that is typically used when you're not intending to use a result for anything. So you're unpacking the results of the git commands into two variables named output and _, but will not use the second (I assume it is exit status or maybe standard error output).

Upvotes: 3

millimoose
millimoose

Reputation: 39980

No, it's not string concatenation. The _ in and of itself doesn't mean anything in Python.

In Python, a function can return more than one value, and you can assign to more than one variable in one statement. Combining these two features lets you write code such as:

def foo():
    return 1, 2, 3

a, b, c = foo()
print(a) # prints 1
print(b) # prints 2
print(c) # prints 3

There's a common convention in languages that support working with multiple values like this that naming one _ means "I don't really care about what ends up in this variable." In your example, the function call_command returns what the command writes to standard output in its first return value, and what's written to standard error in the second. Whoever coded that apparently didn't care about the errors reported by the commands.

The output concatenation you mention must happen elsewhere.

Upvotes: 0

Greg Hewgill
Greg Hewgill

Reputation: 993951

The general form

a, b = x, y

is tuple assignment. The corresponding parts are assigned, so the above is equivalent to:

a = x
b = y

In your case, call_command() returns a tuple of two elements (which is what process.communicate() returns). You're assigning the first one to output and the second one to _ (which is actually a variable name, typically used to name something when you don't care about the value).

Upvotes: 13

jcollado
jcollado

Reputation: 40414

There are two conventions here:

  • Unpack results into a tuple of two elements (,)
  • I don't care about the second element of the tuple so use _ as the name of that variable.

In this particular case, process.communicate returns (stdout, stderr), but the code that calls call_command isn't interested in stderr so it uses this notation to get stdout directly. This would be more or less equivalent to:

result = call_command(<command>)
stdout = result[0]

Upvotes: 4

Related Questions