Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In python, what the underline parameter mean in function

Tags:

python

For example, I read a code:

def parse_doc(self, _, doc):

What does the underline "_" mean?

like image 850
xueliang liu Avatar asked Jul 10 '12 21:07

xueliang liu


2 Answers

It usually is a place holder for a variable we don't care about. For instance if you have a for-loop and you don't care about the value of the index, you can do something like

for _ in xrange(10):
   print "hello World." # just want the message 10 times, no need for index val

another example, if a function returns a tuple and you don't care about one of the values you could use _ to make this explicit. E.g.,

val, _ = funky_func() # "ignore" one of the return values

Aside

Unrelated to the use of '_' in OP's question, but still neat/useful. In the Python shell, '_' will contain the result of the last operation. E.g.,

>>> 55+4
59
>>> _
59
>>> 3 * _
177
>>>
like image 60
Levon Avatar answered Oct 25 '22 14:10

Levon


Like doc it's a variable name. Usually naming a variable _ indicates that it won't be used.

like image 32
ThiefMaster Avatar answered Oct 25 '22 12:10

ThiefMaster