Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Resetting Global Variables?
#1
I will try to put this as explanatory as possible since more questions or replies may/could come from this question, but I have about 400 lines of codes, 13 functions, and about 12 global variables. As I am coming to a close with all the functions working with each other, I notice that when the code finishes without errors, some of my global variables are not being reset. They are retaining their values until a certain period and then finally gives the variable a new value. I will post as much code as I can without giving sensitive data. How would you properly declare a global variable with use for multiple functions as well as re-assigning the variables when needed?

def FindCustName(CustomerSaleID):
    global CustomerName
    global Vin
    global Fnum
    CustomerName = ""
    Vin = str(CustomerIndex[int(float(LoadedSales['CC'][InxLoc]))][2])[-8:]
    FNum= str(CustomerIndex[CustomerSaleID][3])
    if ItemTag == "Lube" or str(CurHA).find("EntPrise") > 0:
        CustomerName = str(CustomerIndex[CustomerSaleID][3])
        return CustomerName
    else:
        CustomerName = str(CustomerIndex[CustomerSaleID][0])
    return CustomerName
global CustomerName
CustomerName = ""


Currently I have been assinging my global variables within the function that they are first called/created from. What is highlighted red is setup because it seems I have to declare some value before using the global variable. In other words, it seems to never let me just declare a global variable-I have to set the value as well. I have no global variables assigned or created outside any functions. Again I have 13 total functions with at least 8 functions having at least one global variable. I feel I am going about this the wrong way. Any suggestions? Thanks.

Note: If possible, I would love to post the entire script with sensitive data removed because I would love to hear everyones opinion on the code. So if this is something that becomes requested frequently, I will do my best to post the entire code.
Reply


Messages In This Thread
Resetting Global Variables? - by WuchaDoin - Sep-28-2018, 03:12 PM
RE: Resetting Global Variables? - by micseydel - Sep-28-2018, 04:31 PM
RE: Resetting Global Variables? - by WuchaDoin - Sep-28-2018, 04:35 PM
RE: Resetting Global Variables? - by ichabod801 - Sep-28-2018, 05:14 PM
RE: Resetting Global Variables? - by WuchaDoin - Sep-28-2018, 05:20 PM
RE: Resetting Global Variables? - by nilamo - Sep-28-2018, 06:41 PM
RE: Resetting Global Variables? - by WuchaDoin - Sep-28-2018, 06:59 PM
RE: Resetting Global Variables? - by nilamo - Sep-28-2018, 07:09 PM
RE: Resetting Global Variables? - by WuchaDoin - Sep-28-2018, 07:50 PM
RE: Resetting Global Variables? - by ichabod801 - Sep-28-2018, 08:34 PM
RE: Resetting Global Variables? - by WuchaDoin - Sep-28-2018, 08:41 PM
RE: Resetting Global Variables? - by volcano63 - Sep-28-2018, 08:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand global variables 357mag 5 1,115 May-12-2023, 04:16 PM
Last Post: deanhystad
  Global variables or local accessible caslor 4 1,021 Jan-27-2023, 05:32 PM
Last Post: caslor
  global variables HeinKurz 3 1,143 Jan-17-2023, 06:58 PM
Last Post: HeinKurz
  Clarity on global variables JonWayn 2 942 Nov-26-2022, 12:10 PM
Last Post: JonWayn
  resetting an iterator to full Skaperen 7 6,960 Feb-20-2022, 11:11 PM
Last Post: Skaperen
  variables vcnt, ocnt, and mcnt adding previous values and not resetting to 0 archanut 2 1,928 Feb-12-2021, 06:56 PM
Last Post: deanhystad
  Global variables not working hobbyist 9 4,719 Jan-16-2021, 03:17 PM
Last Post: jefsummers
  Global vs. Local Variables Davy_Jones_XIV 4 2,650 Jan-06-2021, 10:22 PM
Last Post: Davy_Jones_XIV
  Global - local variables Motorhomer14 11 4,232 Dec-17-2020, 06:40 PM
Last Post: Motorhomer14
  Question regarding local and global variables donmerch 12 5,076 Apr-12-2020, 03:58 PM
Last Post: TomToad

Forum Jump:

User Panel Messages

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