Is this a "gotcha" in Python?

Chris Angelico rosuav at
Sat Apr 20 18:56:17 EDT 2019

On Sun, Apr 21, 2019 at 8:43 AM DL Neil <PythonList at> wrote:
> > Be aware that this is using an old form of Python syntax, not
> > supported by current versions. To try this example in a modern version
> > of Python, write it like this:
> >
> > for l in range(50):
> >      print(l, end=" ")
> Python2: print l,
> Python3: print( l )
> Recommend using the latter, unless "this application" MUST use/import an
> externally-sourced module which has yet to be updated from Python2!

And even then, you can choose to use print as a function in your
module. Helps with compatibility. Anyhow.

> Regarding the choice of variable names/attaching meaning:-
> Isn't there an argument that in this context, using the single letter
> "l" as a variable name is 'lazy'? That the "l" could be used in
> different contexts (per OP). That it conveys no meaning as to the
> variable's purpose?

In this specific case, I actually think that "l" is a bad choice, but
not because it's a single letter - more because there is a very strong
convention of using "i" for a loop iterator, and the lowercase "l" is
confusingly similar.

> Surely the variable actually has 'meaning'. Otherwise it wouldn't be
> used in the print statement/function! (appreciating this is a simple
> situation/'toy example')
> That being the case, "l" should be something like "list_of_choices"?

No; a name like that would imply that it is a *collection*. You could
iterate over such a thing, but the loop iterator gets just one of
them. (Unless you're iterating over a list of lists of choices, or
something unusual like that.)

> There is an opposite case (and I'm somewhat diffident about it). Namely,
> using the underscore as a temporary variable. I have not seen it very
> often (YMMV). However, it seems to fit under the heading of 'a Pythonic
> convention'.

Generally, the lone underscore means "this doesn't matter". A Python
program might do something five times thus (as per your example

for _ in range(5):

But if you want to take notice of WHICH something you're doing, it's
much better to use a different name:

for i in range(5):

> PEP-8 [pep8] talks of <<<_single_leading_underscore: weak "internal use"
> indicator>>> because whilst there is a convention of 'private use' it is
> not enforced as a 'rule' - ie the "consenting adults" clause applies!
> Thus, I have occasionally seen a sole underscore used as a variable
> name. The "weak internal use" seemingly: <<<I need a variable name
> 'here', but I have no use for it.>>>

More or less. I would distinguish the lone underscore from the leading
underscore, but there is a broad parallel.

> # We can even ascertain the (latest) value applied to the _ variable:
>  >>> _
> 3

This is actually a different convention again, as the lone underscore
is used automatically by the REPL:

>>> 1 + 2
>>> _ + 3

> Arguing that the "l" variable name conveys no meaning, or is 'just a
> counter', could one then modify the previous example:
>  > for _ in range( 50 ):
>  >      print( _, end=" " )
> 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
> 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 >>>

IMO this is a dangerous use of the underscore. If you DO care about
the value, it should get another name.

That said, though: the name does NOT need to be long. Single-letter
variable names are entirely valuable. Short names for short-lived
variables with small scope are absolutely fine.


More information about the Python-list mailing list