what does 'for _ in range()' mean?

Terry Reedy tjreedy at udel.edu
Wed Jul 28 14:48:42 EDT 2004


> What convention? I have to agree with a couple
> of other posters; I've never heard of it before.

Coming from Fortran and C, I myself use i for 'don't care' looping vars.

> If it really is a convention,

It is obviously not a universal convention.  What does library code use?

> it would be nice to have it
> documented somewhere (like the single underscore
> in front of a variable means "weak internal use").
> Somewhere is most likely PEP 8 - the Python
> Style Guide.

In the interactive interpreter, _ is bound to the last expression evaluated
that is otherwise unassigned.

>>> a=3
>>> _
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name '_' is not defined
>>> 3
3
>>> _
3

This is the only official use of _ that I know of and it is documented
somewhere.

Terry J. Reedy






More information about the Python-list mailing list