'_' and '__'
Chris Angelico
rosuav at gmail.com
Sat May 26 19:46:01 EDT 2018
On Sun, May 27, 2018 at 9:40 AM, Thomas Jollans <tjol at tjol.eu> wrote:
> On 27/05/18 01:19, Mike McClain wrote:
>> In their discussion of 'List replication operator'
>> Steven D'Aprano and Ben Finney used these '_' and '__'.
>> Steve said, "[[] for _ in range(5)]".
>> Ben said, "[ [] for __ in range(5) ]".
>>
>> These aren't listed separately in the index, where might I find
>> written discussion of these?
>
> There's nothing special about _, it's just a possible name of a
> variable, albeit a very short and entirely uninformative one. Normally,
> it's not something you'd actually want to name a variable, of course.
>
> As such, _ has become an idiomatic name for dummy variables, i.e.
> something you use when syntax requires you to give a variable name, but
> you don't actually want one (probably because you're throwing the
> variable away). This mostly happens in generator expressions/list
> comprehensions.
Yes, and also in stand-alone 'for' loops where you don't care about
the iteration variable:
for _ in range(3): datafile.skipline()
The only actual significance of this name is that the interactive
interpreter will stash the result of the previous expression in that
variable.
>>> 6 * 7
42
>>> _ + 1
43
Other than that, it is simply an ordinary name, but one that has some
conventions attached to it.
ChrisA
More information about the Python-list
mailing list