What does underscore mean in Python?

I'm kinda new to Python, so I'm trying to read existing code. I am a bit confused about the syntax of this, though.

For instance:

rlist, _, _ = select.select(sockets, [], [])

I understand that it select.select()accepts 3 lists (and I assume that it []simply means an empty list), but is there _any placeholder to denote it?

+5
source share
5 answers

This is just a variable name! Usually people use _for variables that are temporary or immaterial.

As other people have stated, it _is a common alias for gettext, a translation library. You can determine when it is used as gettext if you see that it is called a function, for example. _('Hello, world!').

Protip: python .

>>> 3 + 4
7
>>> a = _
>>> print a
7
+10

python. i .

, .

python _, python.

+3

, , _ Python. , .

>>> 2+2
4
>>> _+2
6

(, , , Python script , .)

+1

. , , .

0

Typically, you name a variable with a single underscore when you no longer need to refer to that variable. For example, something like this:

for _ in range(10):
    print "hello"

It just prints hi 10 times, and we don’t need to refer to the loop control variable ( _in this case).

In your example, it select.select(sockets, [], [])returns a tuple (or list or set), from which you seem to need only the first element, so you use underscores.

0
source

All Articles