On 21/04/19 8:16 AM, Chris Angelico wrote:
On Sun, Apr 21, 2019 at 2:14 AM Dennis Lee Bieber <wlfr...@ix.netcom.com> wrote:
         Only use short (single character) names for items that only exist as
loop control, and are not rebound within the loop, nor used outside of the
scope of that loop (but can be reused in another subsequent loop
control)...

for l in range(50):
...     print l,
...
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

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!


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?

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"?


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'.

Remember that Python variable names may commence with a letter [pep3] or an underscore [lexi]. Variables commencing with an underscore are described as <<<2.3.2. Reserved classes of identifiers>>>.

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.>>>


Example usage includes:

- accessing a tuple, but not being interested in every element, eg

>>> t = ( 1, 2, 3 )
# We ignore the first element unpacked from the tuple
>>> _, a, b = t
>>> print( f'There are { a } of these and { b } of those' )
There are 2 of these and 3 of those
>>> a
2
>>> b
3
# We can ignore more than one element - but here be dragons!
>>> _, c, _ = t
>>> print( f'{ c } is the only value needed' )
2 is the only value needed
>>> c
2
# We can even ascertain the (latest) value applied to the _ variable:
>>> _
3
# See the need to watch for those dragons!

- ignoring the 'index' of a for-loop, eg

# Let's do a speed test against client requirements
for _ in range( 1000000 ):
    time_something()

- most usually I've seen the two combined:

# Ignore one element's value and process the other
for _, value in list_of_tuples:
    # use value but not _


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 >>>


Would this provoke a discussion (or an argument) in your next code review?


--
Regards =dn

Web-refs:
[lexi] https://docs.python.org/3.6/reference/lexical_analysis.html#identifiers
[pep3] https://www.python.org/dev/peps/pep-3131/
[pep8] https://www.python.org/dev/peps/pep-0008/
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to