Python Style and Naming Variables - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Python Style and Naming Variables (/thread-33898.html) |
Python Style and Naming Variables - rsherry8 - Jun-07-2021 Please consider the following Python code: def my_sum(array): ret = 0 for i in range(len(array)): ret += array[i] return ret array = [ 1,2,3,4] print( "sum is " + str(my_sum(array)) )In this case, the code analyzer from Spyder generates a warning because the name of the parameter and the name of the global variable are the same. I do not see anything wrong with this. Is there something I am missing? RE: Python Style and Naming Variables - Yoriz - Jun-07-2021 I think array is ok but you should iterate the items directly and using f strings is better. def my_sum(array): result = 0 for value in array: result += value return result array = [1, 2, 3, 4] print(f'sum is {my_sum(array)}') RE: Python Style and Naming Variables - snippsat - Jun-07-2021 It's not wrong,i it's a linter message that you can ignore . Here how i would write this code,look at Never use "for i in range(len(sequence)) def my_sum(lst): ret = 0 for index, item in enumerate(lst): ret += item return ret lst = [1, 2, 3, 4] print(f"sum is {my_sum(lst)}") Look into linter eg online PEP8 online and Black Playground So Pylint, Black are tool to hlep you write better code. When use Spyder so is Pylint integrated,can follow the advice or ignore it. Copy my code over into pep8online ,then your code and see there is some advice. RE: Python Style and Naming Variables - deanhystad - Jun-07-2021 The less you reuse names the easier it is to find things by name. In Python you can use the same name for a module variable and a function variable, but there will always be a moment of hesitation when looking at the function. Did I mean to create a new variable in this scope, or did I want to use the global/module scope variable. If you give them different names that hesitation evaporates. There is no hesitation using the same name for global/module variables and function arguments, but it does make it more difficult to search for all occurrences of the global/module variable. If I search for all occurrences of array, some of the hits will be for the global variable and some for the function argument. This doesn't cause any confusion or much trouble for your little example, but what if your module was hundreds of lines long and you reuse "array" as a variable name indiscriminately? Sure, you can depend on you tools to help straighten things out, but why? Why not pick a naming convention now that eliminates the problem? The linting tool I use would complain about a global variable named "array", recommending "ARRAY" instead. I think "array" is a bad name for a global variable. Fine for a local variable or a function argument, but not informative for a global variable. Does it have some important purpose? A more descriptive name might let me read the code without having to resort to finding where you did (or did not) accurately document the purpose of this variable. If it is of no purpose at all, a better name can help with that too. How about "test_array" or "temp_array" or not even making a variable and passing the list directly. print( "sum is " + str(my_sum([1, 2, 3, 4])) ) |