pep8-naming icon indicating copy to clipboard operation
pep8-naming copied to clipboard

Allow some single uppercase letter function arguments and-or local variables

Open pedros opened this issue 12 years ago • 3 comments

The use of single uppercase letter variables is pretty widespread in some applications, namely those that are more math-oriented. For example, X is a common variable name given to a matrix, G for graphs, etc.

My understanding of PEP8 is that these are specifically allowed (see Names to avoid), but also not specifically disallowed in the more relevant Function and method arguments section, which lists only N804 and N805.

pedros avatar Jan 02 '14 15:01 pedros

I think we could make an exception to N806 for single-letter variable names. It would also be easy to implement.

What do the other @PyCQA/pep8-naming-dev folks think about this?

jparise avatar Jan 02 '20 18:01 jparise

The cited sentence is:

Never use the characters 'l' (lowercase letter el), 'O' (uppercase letter oh), or 'I' (uppercase letter eye) as single character variable names.

I would say it's a misinterpretation that it explicitly disallowing single upper case variable names for certain letters, is it implicitly allowing of other letters as upper case to be used as variables.

I think the case where the cited reason comes into effect are cases like class E: (allowed) vs class I: (disallowed).

Instead of special-casing single letters for this specific use case, unless there's good evidence that this is a really common naming pattern, I think we should recommend:

  • --ignore-names for code bases where X and G like OP suggested are common.
  • use per-line disabling like X = 3 # noqa: N806 in codebases where it is uncommon

saifelse avatar Jan 02 '20 19:01 saifelse

  • use per-line disabling like X = 3 # noqa: N806 in codebases where it is uncommon

This suggestion unfortunately isn't great for function argument names, which are relevant to this use case:

def scale_matrix(M, scalar):

... because it would suppress N806 for all names on that line, which could be overly blunt.

jparise avatar Jan 02 '20 19:01 jparise