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 the file as time and date. BettyTurnips 3 614 Jan-15-2021, 07:52 AM
Last Post: BettyTurnips
  Tkinter font style ATARI_LIVE 2 388 Oct-24-2020, 08:22 PM
Last Post: ATARI_LIVE
  naming conventions mvolkmann 4 663 Sep-28-2020, 05:51 PM
Last Post: Gribouillis
  C style strings deanhystad 2 798 May-16-2020, 02:57 PM
Last Post: Larz60+
  How to generate a log in a list style? wagnergt12 5 942 Apr-22-2020, 12:47 PM
Last Post: buran
  Question about naming variables in class methods sShadowSerpent 1 645 Mar-25-2020, 04:51 PM
Last Post: ndc85430
  Type hints and style MaxPowers 1 759 Feb-19-2020, 06:56 PM
Last Post: micseydel
  Dyanmically Naming Files ovidius 3 873 Jan-29-2020, 02:44 PM
Last Post: buran
  naming images adding to number within multiple functions Bmart6969 0 751 Oct-09-2019, 10:11 PM
Last Post: Bmart6969
  Sub: Python-3: Better Avoid Naming A Variable As list ? adt 9 1,366 Aug-29-2019, 08:15 AM
Last Post: adt

Forum Jump:

User Panel Messages

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