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

I am learning Python and reading an example script that includes some variable definitions that look like this:

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 the output, I get the result of the shell, and I know that it combines the variables. But I can not find the link to the agreement _ in any of the documents. Can someone explain this to me so that I know for sure that I use it correctly?

+6
source share
5 answers

General form

 a, b = x, y 

- purpose of the tuple. The relevant 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 returns process.communicate() ). You assign the first digit to output , and the second to _ (which is actually the name of the variable, usually used to mean something when you don't care about the value).

+13
source

There are two conventions here:

  • Unzip the results into a tuple of two elements ( , )
  • I don't need the second element of the tuple, so use _ as the name of this variable.

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

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

_ is a valid variable name in Python, which is usually used when you are not going to use the result for anything. This way, you decompress the results of git commands into two variables named output and _ , but you will not use the second (I assume this is the exit status or, possibly, the standard error output).

+3
source

You also see this in perl with undef instead of _ .

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

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

+2
source

No, this is not string concatenation. _ By itself does not mean anything in Python.

In Python, a function can return more than one value, and you can assign more than one variable in a single expression. Combining these two functions allows you to write code, for example:

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

There is a general convention in languages ​​that support working with multiple values, such as naming _ means that "I really don't care what ends up with this variable." In your example, the call_command function returns what the command writes to standard output in its first return value, and what the standard error says in the second. Whoever encoded that apparently didn't care about the errors reported by the teams.

The concatenation of the output you mention must occur elsewhere.

0
source

Source: https://habr.com/ru/post/907261/


All Articles