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.
"Convention" or "Tradition"?
(cue sound-track from "Fiddler on the Roof")
Back in the days of (my) learning FORTRAN (back then I suspect it did
not even have a (version) number), the rule was that any variable name
starting with a letter between I and N was an integer, and all else were
reals/floating-point. Later, the REAL and INTEGER type declarations were
introduced - but readability suffered when a dozen pages 'down' one read
ABC = 10 (huh?), having read INTEGER ABC way-back!
(those particular letters chosen because of IN-teger, for those who
didn't notice!)
IIRC it was COBOL Data Division entries and FORTRAN FORMAT statements
which introduced the place-holder convention/tradition of A for alpha, 9
for digit, and X for alphanum. Apparently less-widely observed these days.
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.)
Agreed - no idea what the OP's real-use might be.
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'.
for i in range(5):
do_something(i)
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!
More or less. I would distinguish the lone underscore from the leading
underscore, but there is a broad parallel.
Generally, the lone underscore means "this doesn't matter". A Python
program might do something five times thus (as per your example...
But if you want to take notice of WHICH something you're doing, it's
much better to use a different name:
+1
Farm kids are often taught: don't name something if you're going to
end-up eating it later.
Recommend that Python trainees be taught: name everything that has a use
later...
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.
+1
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.
Upon first coming across the "_" place-holder idea, I started to use it
(perhaps over-use it?). However, rapidly learned/noticed the virtue of
"use => name".
Unfortunately, sometimes dreaming-up a name seems like a lot of effort.
So, I found myself wanting to say "counter", "pointer", "index"...
(whilst at the same time being most thankful of Python's for-each
construct removing the many use-cases for the for/do-loop by-index
prevalent in other coding-languages, and the source of so many errors!).
Despite the fact that most text-editors will now 'guess' varNMs for us,
'laziness' ensues, just as it does in email/SMS/etc. Thus "ctr", "ptr",
"ndx"... and pretty soon, we are back to "i". Cry it from the roof-tops:
TRADITION!
Yet, because "i" (or "n", "a", or "x"...) does not convey usage-meaning
- other than, "I am a place-holder"! So, aren't we back to "_"?
--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list