Python Forum
Python Style and Naming Variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Style and Naming Variables
#1
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?
Reply
#2
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)}')
Reply
#3
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)}")
Output:
sum is 10
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.
Reply
#4
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])) )
Gribouillis likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  naming entities(city abbreviations) tirumalaramakrishna 1 1,196 May-06-2022, 11:22 AM
Last Post: jefsummers
  Naming the file as time and date. BettyTurnips 3 2,887 Jan-15-2021, 07:52 AM
Last Post: BettyTurnips
  naming conventions mvolkmann 4 2,094 Sep-28-2020, 05:51 PM
Last Post: Gribouillis
  Question about naming variables in class methods sShadowSerpent 1 1,960 Mar-25-2020, 04:51 PM
Last Post: ndc85430
  Dyanmically Naming Files ovidius 3 2,489 Jan-29-2020, 02:44 PM
Last Post: buran
  naming images adding to number within multiple functions Bmart6969 0 1,892 Oct-09-2019, 10:11 PM
Last Post: Bmart6969
  Sub: Python-3: Better Avoid Naming A Variable As list ? adt 9 3,936 Aug-29-2019, 08:15 AM
Last Post: adt
  Naming convention advice Alfalfa 5 3,276 Jul-21-2018, 11:47 AM
Last Post: Larz60+
  Python Naming Error: List not defined Intelligent_Agent0 1 14,268 Mar-13-2018, 08:34 PM
Last Post: nilamo
  Coding style questions -- variables Luke_Drillbrain 12 9,007 Apr-30-2017, 01:21 AM
Last Post: Luke_Drillbrain

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020