Python Forum
Python Style and Naming Variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Style and Naming Variables
#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


Messages In This Thread
Python Style and Naming Variables - by rsherry8 - Jun-07-2021, 09:00 PM
RE: Python Style and Naming Variables - by Yoriz - Jun-07-2021, 09:11 PM
RE: Python Style and Naming Variables - by snippsat - Jun-07-2021, 09:30 PM
RE: Python Style and Naming Variables - by deanhystad - Jun-07-2021, 09:30 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  naming entities(city abbreviations) tirumalaramakrishna 1 1,250 May-06-2022, 11:22 AM
Last Post: jefsummers
  Naming the file as time and date. BettyTurnips 3 2,990 Jan-15-2021, 07:52 AM
Last Post: BettyTurnips
  naming conventions mvolkmann 4 2,166 Sep-28-2020, 05:51 PM
Last Post: Gribouillis
  Question about naming variables in class methods sShadowSerpent 1 2,021 Mar-25-2020, 04:51 PM
Last Post: ndc85430
  Dyanmically Naming Files ovidius 3 2,541 Jan-29-2020, 02:44 PM
Last Post: buran
  naming images adding to number within multiple functions Bmart6969 0 1,928 Oct-09-2019, 10:11 PM
Last Post: Bmart6969
  Sub: Python-3: Better Avoid Naming A Variable As list ? adt 9 4,035 Aug-29-2019, 08:15 AM
Last Post: adt
  Naming convention advice Alfalfa 5 3,350 Jul-21-2018, 11:47 AM
Last Post: Larz60+
  Python Naming Error: List not defined Intelligent_Agent0 1 14,314 Mar-13-2018, 08:34 PM
Last Post: nilamo
  Coding style questions -- variables Luke_Drillbrain 12 9,155 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