Why does Pylint object to single-character variable names?

后端 未结 5 1743
我寻月下人不归
我寻月下人不归 2021-01-30 12:08

I\'m still getting used to Python conventions and using Pylint to make my code more Pythonic, but I\'m puzzled by the fact that Pylint doesn\'t like single character variable na

5条回答
  •  说谎
    说谎 (楼主)
    2021-01-30 12:41

    In strongly typed languages, one-letter name variables can be ok-ish, because you generally get the type next to the name in the declaration of the variable or in the function / method prototype:

    bool check_modality(string a, Mode b, OptionList c) {
        ModalityChecker v = build_checker(a, b);
        return v.check_option(c);
    }
    

    In Python, you don't get this information, so if you write:

    def check_modality(a, b, c):
        v = build_checker(a, b)
        return v.check_option(c)
    

    you're leaving absolutely no clue for the maintenance team as to what the function could be doing, and how it is called, and what it returns. So in Python, you tend to use descriptive names:

    def check_modality(name, mode, option_list):
        checker = build_checker(name, mode)
        return checker.check_option(option_list)
    

    And you even add a docstring explaining what the stuff does and what types are expected.

提交回复
热议问题